/[perl]/sort_bindzone.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sort_bindzone.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Thu May 8 14:58:24 2003 UTC (20 years, 10 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/plain
sort bind zone

1 dpavlin 1.1 #!/usr/bin/perl -w
2     #
3     # Sort bind zone file by IP number
4     #
5     # 2003-05-03 Dobrica Pavlinusic <dpavlin@rot13.org>
6     #
7     # parse files in format
8     #
9     # name A 111.222.333.444
10     # name CNAME name
11     #
12     # and output sorted list
13    
14     use strict;
15    
16     my %a_rec;
17     my %cname;
18    
19     while(<>) {
20     chomp;
21     my ($name,$type,$ip) = split (/\s+/,$_,3);
22     if (lc($type) eq "a") {
23     if ($ip =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
24    
25     my $i = ((((($1 * 255 + $2) * 255) + $3) * 255) + $4) * 100;
26     while ($a_rec{$i}) { $i++; }
27     #print "$i: $name $type $ip\n";
28     $a_rec{$i} = [ $name, $type ,$ip ];
29     } else {
30     print "; can't parse IP: $ip -- original line below:\n";
31     print "; $name\t$type\t$ip\n";
32     }
33     } elsif (lc($type) eq "cname") {
34     $cname{$ip} .= $name."\t";
35     } else {
36     print "; unknown type: $type -- original line below:\n";
37     print "; $name\t$type\t$ip\n";
38     }
39     }
40    
41     print ";\n";
42     my $netmask_regex='^\d+\.\d+'; # bclass
43     my $net = "";
44    
45     foreach my $key (sort(keys %a_rec)) {
46     my ($name,$type,$ip) = @{$a_rec{$key}};
47    
48     if ($ip =~ /($netmask_regex)/) {
49     if ($1 ne $net) {
50     $net = $1;
51     print ";\n; network $net\n;\n";
52     }
53     } else {
54     print "; can't parse $ip to find network!\n";
55     }
56    
57     # pretty format record
58     sub dump_rec {
59     my $name = shift @_;
60     print "$name\t";
61     print "\t" if (length $name < 8);
62     print shift @_,"\t",shift @_,"\n";
63     }
64    
65     # dump a
66     dump_rec($name,$type,$ip);
67    
68     # dump cname if exists
69     if ($cname{$a_rec{$key}[0]}) {
70     foreach my $alias (split(/\t/,$cname{$a_rec{$key}[0]})) {
71     undef $cname{$a_rec{$key}[0]};
72     dump_rec($alias,"CNAME",$a_rec{$key}[0]);
73     }
74     }
75     }
76    
77     print "; CNAME to CNAME\n";
78     foreach my $alias (keys %cname) {
79     next if (! $cname{$alias});
80     foreach my $host (split(/\t/,$cname{$alias})) {
81     dump_rec($host,"CNAME",$alias);
82     }
83     }

  ViewVC Help
Powered by ViewVC 1.1.26