#!/usr/bin/perl #named muss laufen; localhost (zone 127.0.0.0) wird nicht bearbeitet, #da konstant. named\.(boot|conf) liegt als Beispiel bei. #const #$pz="/var/named/pz" ; #$hostsfile="/etc/hosts" ; $pz="/raid/tmp/pz" ; $hostsfile="/raid/tmp/hosts.yp" ; #in Hosts: nur einmal IP (am Zeilenanfang, nicht doppelt) #erste Eintrag ist Realname, rest: CNAME #forward: sub dnsheader; sub getmtime; #vars: #$IP = IP - Nummer #$NAMES = namen aus $hostsfile #%HOSTS = {$IP}=$NAMES #@PREREC = $NAMES als Liste (tmp) #@NAMESREC = $NAMES formatiert, ohne doppelte #%DNS = {$IP}=@REC , RECS: A, CNAME, CNAME,... #first check time-stamps: exit if ((&getmtime("$hostsfile") < &getmtime("$pz/tfh-berlin.de")) and (&getmtime("$hostsfile") < &getmtime("$pz/64.141.in-addr.arpa"))); open (HOSTS,$hostsfile); while () { next if ! /^141\.64\./ ; #nur tfh-domain if (/^(141\.64\.\d+\.\d+)\s+(.+)$/) { my $IP = $1; #IP - Nummer my $NAMES = $2; #Namen aus .../hosts $NAMES =~ s/\.tfh-berlin\.de//g; #domain raus $NAMES =~ s/\s+/ /g; #space fuer whitespaces $NAMES =~ s/\s+$//; #aber nicht am ende die "IP($IP) mehrfach!!\n" if defined $HOSTS{$IP}; #hamma schon $HOSTS{$IP} = $NAMES; #ansonsten merken } else { die "IP-Parse-Fehler!\n"; #keine richtige IP-Nummer } } close (HOSTS); #Now let's built the %DNS Table while (($IP,$NAMES) = each %HOSTS) { my @PREREC; #Namenliste unbearbeitet my @NAMESREC; #Namenliste: 1. realname rest:CNAME @PREREC = split (" ", $NAMES); #space = delimiter, also " host " foreach $NAME (@PREREC) { my $nametemp = " " . join(' ',@NAMESREC) . " "; if ($nametemp !~ / $NAME /) { #noch nicht in @NAMESREC? push (@NAMESREC,$NAME); #dann rein damit! } } $DNS{$IP} = join(' ',@NAMESREC); #@NAMESREC enthaelt keine doppelten mehr } open (STDOUT, ">$pz/tfh-berlin.de") or die "Couldn't create: $pz/tfh-berlin.de\n"; #Now lets export our table print ";Zone: .tfh-berlin.de."; &dnsheader; while (($IP,$DNSREC) = each %DNS) { @NAMESREC = split (" ", $DNSREC); #space = delimiter, also " host " printf "%-20s A %-20s\n",$NAMESREC[0],$IP; #erster ist A: foreach $NAME (@NAMESREC[1..$#NAMESREC]) { printf "%-20s CNAME %-20s\n",$NAME,$NAMESREC[0]; #rest ist CNAME: } } close (STDOUT); #Reverse Lookup zone file: open (STDOUT, ">$pz/64.141.in-addr.arpa") or die "Couldn't create: $pz/64.141.in-addr.arpa\n"; print ";Zone: 64.141.in-addr.arpa (for tfh-berlin.de)\n"; &dnsheader; while (($IP,$DNSREC) = each %DNS) { @NAMESREC = split (" ", $DNSREC); #space = delimiter, also " host " $REVIP = $IP; $REVIP =~ s/^141\.64\.//; $REVIP =~ s/(\d+)\.(\d+)/$2.$1/; printf "%-20s PTR %20s.tfh-berlin.de.\n",$REVIP,$NAMESREC[0]; #erster ist A: $IP=""; } close (STDOUT); print `killall -v -HUP named`; die "Zone files updated.\n"; #Bodies der subs: sub getmtime { local($filename) = pop(@_); if (-r $filename) { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat ($filename); } else { return 1; } return $mtime; } sub dnsheader { $now = `date +%Y:%m:%d:%T`; $now = join("",split("[/:]",$now)); chop($now); print <