📄 h2n
字号:
#!/usr/bin/perl#NAME## h2n - Translate host table to name server file format# $Date: 93/02/04 17:43:05 $ $Revision: 1.16 $##SYNOPSIS## h2n -d DOMAIN -n NET [options]# Various defaults$Host = `hostname`;chop($Host);$Host =~ s/\..*//; $doaliases = 1;$domx = 1;$dowks = 0;$dotxt = 0;$dontdodomains = 0;$Bootfile = "./named.boot";$Hostfile = "/etc/hosts";$Commentfile = "";$Commentfileread = 0;$User = "root";$Pwd = `pwd`;$RespHost = "";$RespUser = "";$DefSerial = 1;$DefRefresh = 10800;$DefRetry = 3600;$DefExpire = 604800;$DefTtl = 86400;$DefMxWeight = 10;$Defsubnetmask = "";$ForceSerial = -1;push(@bootmsgs, "primary\t0.0.127.IN-ADDR.ARPA db.127.0.0\n");&PARSEARGS(@ARGV);&FIXUP;open(HOSTS, $Hostfile) || die "can not open $Hostfile";LINE: while(<HOSTS>){ next if /^#/; # skip comment lines next if /^$/; # skip empty lines chop; # remove the trailing newline tr/A-Z/a-z/; # translate to lower case ($data,$comment) = split('#', $_, 2); ($addr, $names) = split(' ', $data, 2); if ($names =~ /^[ \t]*$/) { print STDERR "Bad line in hosts file ignored '$_'\n"; next LINE; } # Match -e args foreach $netpat (@elimpats){ next LINE if (/[.\s]$netpat/); } # Process -c args foreach $netpat (@cpats){ if (/\.$netpat/) { ($canonical, $aliases) = split(' ', $names, 2); $canonical =~ s/\.$netpat//; if($Cnames{$canonical} != 1){ printf DOMAIN "%-20s IN CNAME %s.%s.\n", $canonical, $canonical, $cpatrel{$netpat}; $Cnames{$canonical} = 1; } next LINE; } } # Check that the address is in the address list. $match = 'none'; foreach $netpat (@Netpatterns){ $match = $netpat, last if ($addr =~ /^$netpat\./); } next if ($match eq 'none'); ($canonical, $aliases) = split(' ', $names, 2); # separate out aliases next if ($dontdodomains && $canonical =~ /\./); # skip domain names $canonical =~ s/$Domainpattern//; # strip off domain if there is one $Hosts{$canonical} .= $addr . " "; # index addresses by canonical name $Aliases{$addr} .= $aliases . " "; # index aliases by address $Comments{"$canonical-$addr"} = $comment; # Print PTR records $file = $Netfiles{$match}; printf $file "%-30s\tIN PTR %s.%s.\n", &REVERSE($addr), $canonical, $Domain;}## Go through the list of canonical names.# If there is more than 1 address associated with the# name, it is a multi-homed host. For each address # look up the aliases since the aliases are associated # with the address, not the canonical name.#foreach $canonical (keys %Hosts){ @addrs = split(' ', $Hosts{$canonical}); $numaddrs = $#addrs + 1; foreach $addr (@addrs) { # # Print address record for canonical name. # if($Cnames{$canonical} != 1){ printf DOMAIN "%-20s IN A %s\n", $canonical, $addr; } else { print STDERR "$canonical - can't create A record because CNAME exists for name.\n"; } # # Print cname or address records for each alias. # If this is a multi-homed host, print an address # record for each alias. If this is a single address # host, print a cname record. # if ($doaliases) { @aliases = split(' ', $Aliases{$addr}); foreach $alias (@aliases){ # # Skip over the alias if the alias and canonical # name only differ in that one of them has the # domain appended to it. # next if ($dontdodomains && $alias =~ /\./); # skip domain names $alias =~ s/$Domainpattern//; if($alias eq $canonical){ next; } if($numaddrs > 1){ printf DOMAIN "%-20s IN A %s\n", $alias, $addr; } else { # # Flag aliases that have already been used # in CNAME records or have A records. # if(($Cnames{$alias} != 1) && (!$Hosts{$alias})){ printf DOMAIN "%-20s IN CNAME %s.%s.\n", $alias, $canonical, $Domain; $Cnames{$alias} = 1; } else { print STDERR "$alias - CNAME or A exists already; alias ignored\n"; } } } } } if ($domx) { &MX($canonical, @addrs); } if ($dotxt) { &TXT($canonical, @addrs); } if ($Commentfile ne "") { &DO_COMMENTS($canonical, @addrs); }}# Deal with spcl'sif (-r "spcl.$Domainfile") { print DOMAIN "\$INCLUDE spcl.$Domainfile\n";}foreach $n (@Networks) { if (-r "spcl.$n") { $file = "DB.$n"; print $file "\$INCLUDE spcl.$n\n"; }}# generate boot.* files&GEN_BOOT;exit;## Generate resource record data for# strings from the commment field that# are found in the comment file (-C).#sub DO_COMMENTS { local($canonical, @addrs) = @_; local(*F, @c, $c, $a, $comments); if (!$Commentfileread) { open(F, $Commentfile) || die "Unable to open file $Commentfile: $!"; $Commentfileread++; while (<F>) { chop; ($key, $c) = split(':', $_, 2); $CommentRRs{$key} = $c; } close(F); } foreach $a (@addrs) { $key = "$canonical-$a"; $comments .= " $Comments{$key}"; } @c = split(' ', $comments); foreach $c (@c) { if($CommentRRs{$c}){ printf DOMAIN "%-20s %s\n", $canonical, $CommentRRs{$c}; } }}## Generate MX record data#sub MX { local($canonical, @addrs) = @_; local($first, $a, $key, $comments); if($Cnames{$canonical}){ print STDERR "$canonical - can't create MX record because CNAME exists for name.\n"; return; } $first = 1; foreach $a (@addrs) { $key = "$canonical-$a"; $comments .= " $Comments{$key}"; } if ($comments !~ /\[no smtp\]/) { # Add WKS if requested if ($dowks) { foreach $a (@addrs) { printf DOMAIN "%-20s IN WKS %s TCP SMTP\n", $canonical, $a; } } printf DOMAIN "%-20s IN MX %s %s.%s.\n", $canonical, $DefMxWeight, $canonical, $Domain; $first = 0; } if ($#Mx >= 0) { foreach $a (@Mx) { if ($first) { printf DOMAIN "%-20s IN MX %s\n", $canonical, $a; $first = 0; } else { printf DOMAIN "%-20s IN MX %s\n", "", $a; } } }}## Generate TXT record data#sub TXT { local($canonical, @addrs) = @_; local($a, $key, $comments); foreach $a (@addrs) { $key = "$canonical-$a"; $comments .= " $Comments{$key}"; } $comments =~ s/\[no smtp\]//g; $comments =~ s/^\s*//; $comments =~ s/\s*$//; if ($comments ne "") { printf DOMAIN "%s IN TXT \"%s\"\n", $canonical, $comments; }}## Create the SOA record at the beginning of the file#sub MAKE_SOA { local($fname, $file) = @_; local($s); if ( -s $fname) { open($file, "$fname") || die "Unable to open $fname: $!"; $_ = <$file>; chop; if (/\($/) { if (! $soa_warned) { print STDERR "Converting SOA format to new style.\n"; $soa_warned++; } if ($ForceSerial > 0) { $Serial = $ForceSerial; } else { ($Serial, $junk) = split(' ', <$file>, 2); $Serial++; } if (!defined($Refresh)) { ($Refresh, $junk) = split(' ', <$file>, 2); ($Retry, $junk) = split(' ', <$file>, 2); ($Expire, $junk) = split(' ', <$file>, 2); ($Ttl, $junk) = split(' ', <$file>, 2); } } else { split(' '); if ($#_ == 11) { if ($ForceSerial > 0) { $Serial = $ForceSerial; } else { $Serial = ++@_[6]; } if (!defined($Refresh)) { $Refresh = @_[7]; $Retry = @_[8]; $Expire = @_[9]; $Ttl = @_[10]; } } else { print STDERR "Improper format SOA in $fname.\n"; print STDERR "I give up ... sorry.\n"; exit(1); } } close($file); } else { if ($ForceSerial > 0) { $Serial = $ForceSerial; } else { $Serial = $DefSerial; } if (!defined($Refresh)) { $Refresh = $DefRefresh; $Retry = $DefRetry; $Expire = $DefExpire; $Ttl = $DefTtl; } close($file); } open($file, "> $fname") || die "Unable to open $fname: $!"; print $file "\@ IN SOA $RespHost $RespUser "; print $file "( $Serial $Refresh $Retry $Expire $Ttl )\n"; foreach $s (@Servers) { print $file " IN NS $s\n"; } print $file "\n";}## Reverse the octets of an IP address and append# in-addr.arpa.#sub REVERSE { join('.', reverse(split('\.', $_[0]))) . '.IN-ADDR.ARPA.';}#
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -