Return to BSD News archive
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!bunyip.cc.uq.oz.au!munnari.oz.au!spool.mu.edu!howland.reston.ans.net!cs.utexas.edu!uunet!zib-berlin.de!irz401!uriah.sax.de!not-for-mail
From: j@uriah.sax.de (J Wunsch)
Newsgroups: comp.os.386bsd.questions
Subject: Re: How to convert /etc/hosts to /etc/named.db
Date: 26 Oct 1994 14:37:00 +0100
Organization: Private U**X site; member IN e.V.
Lines: 116
Message-ID: <38lm1sINN117@bonnie.sax.de>
References: <CxHyLr.BIH@un.seqeb.gov.au>
NNTP-Posting-Host: bonnie.sax.de
pc012@un.seqeb.gov.au (Patrick Collins) writes:
>I want to configure a FreeBSD system as a domain name server. Is there an
>easy way to convert a large /etc/hosts file to the format required for
>/etc/named.db automatically or do we just use one of the text processing
>tools. Does anyone have any scripts written ?
The O'Reilly book says there is one, but i forgot where to find it.
Anyway, 'tis a nice homework for ``How do i use perl'':-) The script
is assuming that you're running it on the machine that is intended to
become the name server. It writes two files, one for the domain
resolution, one for the reverse lookup. Of course, no kind of
warranty etc. blahblah:
#!/usr/bin/perl
open(H, "/etc/hosts") || die "Cannot read /etc/hosts\n";
open(F1, ">hosts.db") || die "Cannot create hosts.db\n";
open(F2, ">rev.db") || die "Cannot create rev.db\n";
$hostname = `hostname`; ## should be FQDN
chop $hostname;
$domain = $hostname;
$domain =~ s/^[^.]*\.//;
$user = getlogin;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$serial = sprintf("%02d%02d%02d01", $year, $mon+1, $mday);
($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($hostname);
($q1,$q2,$q3,$q4) = unpack("CCCC", $addrs[0]);
if($hostname !~ /\./) {
print STDERR
"Your hostname doesn't appear to be an FQDN, enter domain: ";
$domain = <>;
chop $domain;
$hostname = "$hostname.$domain";
}
print F1 <<"EOF";
;
\$ORIGIN $domain.\tIN\tSOA\t$hostname $user.$hostname (
\t\t\t\t$serial ; serial
\t\t\t\t 3600 ; refresh (once an hour)
\t\t\t\t 600 ; retry (after 10 minutes)
\t\t\t\t1209600 ; expire (after 14 days w/o update)
\t\t\t\t 86400); minimum (default time-to-live 1 day)
;
\t\t\tIN\tNS\t$hostname.
*\t\t\tIN\tMX\t10\tmail.$domain.
;
EOF
print F2 <<"EOF";
;
\$ORIGIN $q3.$q2.$q1.IN-ADDR.ARPA.\tIN\tSOA\t$hostname $user.$hostname (
\t\t\t\t$serial ; serial
\t\t\t\t 3600 ; refresh (once an hour)
\t\t\t\t 600 ; retry (after 10 minutes)
\t\t\t\t1209600 ; expire (after 14 days w/o update)
\t\t\t\t 86400); minimum (default time-to-live 1 day)
;
\t\t\tIN\tNS\t$hostname.
;
EOF
while(<H>) {
next if /^[ \t]*#/;
($addr, $primary, @aliases) = split;
if($primary =~ /\./) {
# name containing dots
($nm,$dom) = split(/\./, $primary, 2);
if($dom ne $domain) {
print STDERR "Warning: $primary is outside your domain\n";
if(!$warned) {
print STDERR "(and hence probably outside your zone of " .
"authority)\n";
$warned = 1;
}
}
$primary .= '.';
}
&justify($primary);
print F1 "IN\tA\t$addr\n";
# introduce a primary mail exchanger
print F1 "\t\t\tIN\tMX\t10\tmail.$domain.\n";
print F1 "\t\t\tIN\tHINFO\tCPU\tOS\n;\n";
foreach $alias (@aliases) {
next if "$primary.$domain" eq $alias;
&justify($alias);
print F1 "IN\tCNAME\t$primary\n";
}
print F1 ";\n";
# now the IN-ADDR.ARPA entry
($q11,$q12,$q13,$q14) = split(/\./, $addr);
next if $q1 != $q11 || $q2 != $q12 || $q3 != $q13;
$primary = "$primary.$domain." unless $primary =~ /\./;
print F2 "$q14\t\t\tIN\tPTR\t$primary\n";
}
sub justify
{
local ($name) = @_;
$name .= '.' if $name =~ /\..*[^.]$/;
$i = length($name) / 8;
print F1 $name . "\t" x (4 - $i);
}
--
cheers, J"org work: joerg_wunsch@tcd-dresden.de
private: joerg_wunsch@uriah.sax.de
Steinbach's Guideline for Systems Programming:
Never test for an error condition you don't know how to handle.