File: build-unbound-localzone-from-hosts.pl

package info (click to toggle)
unbound 1.4.6-1%2Bsqueeze3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 15,700 kB
  • ctags: 5,590
  • sloc: ansic: 56,606; sh: 10,753; yacc: 1,100; python: 1,086; makefile: 447; perl: 141
file content (67 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl -WT

use strict;
use warnings;

my $hostsfile = '/etc/hosts';
my $localzonefile = '/etc/unbound/localzone.conf.new';

my $localzone = 'example.com';

open( HOSTS,"<${hostsfile}" ) or die( "Could not open ${hostsfile}: $!" );
open( ZONE,">${localzonefile}" ) or die( "Could not open ${localzonefile}: $!" );

print ZONE "server:\n\n";
print ZONE "local-zone: \"${localzone}\" transparent\n\n";

my %ptrhash;

while ( my $hostline = <HOSTS> ) {

	# Skip comments
	if ( $hostline !~ "^#" and $hostline !~ '^\s+$' ) {

		my @entries = split( /\s+/, $hostline );

		my $ip;

		my $count = 0;
		foreach my $entry ( @entries ) {
			if ( $count == 0 ) {
				$ip = $entry;
			} else {

				if ( $count == 1) {

					# Only return localhost for 127.0.0.1 and ::1
					if ( ($ip ne '127.0.0.1' and $ip ne '::1') or $entry =~ 'localhost' ) {
						if ( ! defined $ptrhash{$ip} ) {
							$ptrhash{$ip} = $entry;
							print ZONE "local-data-ptr: \"$ip $entry\"\n";
						}
					}

				}

				# Use AAAA for IPv6 addresses
				my $a = 'A';
				if ( $ip =~ ':' ) {
					$a = 'AAAA';
				}

				print ZONE "local-data: \"$entry ${a} $ip\"\n";

			}
			$count++;
		}
		print ZONE "\n";


	}
}




__END__