[comp.lang.perl] named -> hosts code anyone?

dnb@meshugge.media.mit.edu (David N. Blank) (04/10/91)

Howdy-
  Before I go reinvent the triangle, I was wondering if anyone has
written the perl code to take a named-style formatted file and convert
it to a hosts file that they wouldn't mind sharing?  I also wouldn't
turn down code to go the reverse way.  Thanks.
        Peace,
            dNb

louie@sayshell.umd.edu (Louis A. Mamakos) (04/11/91)

I have a perl script that's pretty close.  It take domain zone file and
turns them into HOSTS.TXT formatted file; you can then run htable if
you want /etc/hosts flavored files.

This is really, really old perl code, some of my first.  It seems to work
for us, and I've never got around to cleaning it up any.

louie

#!/usr/local/bin/perl
#
$host = '';
$addr = '';
$machine = '';
$opsys = '';
$services = '';
$origin = '';
loop: while(<>) {
	chop;
	if (/.*\($/) {
		$foo = $_;
	 paren:	while (<>) {
			chop;
			$foo .= $_;
			if ($foo =~ /\)$/) {
				last paren;
			}
		}
		$_ = $foo;
	}
	#
	# if the beginning of the line is blank, then substitute the last
	# hostname used.
	#
	s/^([ \t])(.*)$/$host\t$2/;
	@rr = split;

	if ( $rr[0] eq '$ORIGIN' ) {
		if ( $rr[1] !~ /\.$/ ) {
			$origin = $rr[1] . '.' . $origin;
			$origin =~ s/\.\./\./g;
			$origin =~ s/(..*)\.$/$1/;
			printf "; new origin (rel) %s\n", $origin;
		} else {
			$origin = $rr[1];
			$origin =~ s/(..*)\.$/$1/;
			printf "; new origin %s\n", $origin;
		}
		next loop;
	}
	if ( $rr[1] ne 'IN' ) {
		printf ";; %s\n", $_;
		next loop;
	}

	next if ($rr[2] eq "CNAME" );
	next if ($rr[2] eq "NS" );

	if ($rr[2] eq "SOA" ) {
		printf ";\n; domain %s zone transfer, zone serial number %s\n",
			$rr[0] . '.' . $origin, $rr[7];
		printf "; created %s;\n", `date`;
		next loop;
	}

	if ( $host ne $rr[0] ) {
		if ($addr && $host) {
			printf "HOST : %s : %s : %s : %s : : \n", $addr,
				$hostname, $machine, $opsys;
		}
		$addr = '';
		$machine = '';
		$services = '';
		$opsys = '';
		$host = $rr[0];
		$hostname = $host . "." . $origin;
		$hostname =~ s/^(.*)\.$/$1/ ;
	}
	if ($rr[2] eq "A" ) {
		$addr = $rr[3];
		next;
	}
	if ($rr[2] eq "HINFO" ) { 
		$rr[3] =~ s/^\"(.*)\"$/$1/;	# strip off quotes
		$rr[4] =~ s/^\"(.*)\"$/$1/;
		$machine = $rr[3];
		$opsys = $rr[4];
		next;
	}
		
} 
if ($addr) {
	printf "HOST : %s : %s : %s : %s : %s : \n", $addr, $hostname,
		$machine, $opsys, $services;
}

hakanson@ogicse.ogi.edu (Marion Hakanson) (04/12/91)

Randal was good enough to put David in touch with me directly, but
others might want to try out my C and Perl solution to this task.
It's available for anonymous FTP from host cse.ogi.edu, in
pub/dnsparse-2.0.tar.Z.

Note that this includes a subroutine which is a general parser of DNS
master files, plus some programs which use the parser to produce
interesting things: a hosts-style file, and a primitive set of PTR
records based on A records, to name a couple.

I apologize in advance for the lack of documentation, but the tools
(especially "dns2hosts") are robust and have served us well for quite
some time.

-- 
Marion Hakanson         Domain: hakanson@cse.ogi.edu
                        UUCP  : {hp-pcd,tektronix}!ogicse!hakanson

kenr@cruise.cc.rochester.edu (Kenneth C. Rich) (04/15/91)

In article <DNB.91Apr10114112@meshugge.media.mit.edu> dnb@meshugge.media.mit.edu (David N. Blank) writes:
>  Before I go reinvent the triangle, I was wondering if anyone has
>written the perl code to take a named-style formatted file and convert
>it to a hosts file that they wouldn't mind sharing?  I also wouldn't
>turn down code to go the reverse way.  Thanks.

I just sent our dns-to-hosts.txt-format item.
It works great, but is a little hardwired to our situation.
Being the 2nd thing I ever did in perl, I might be embarrassed if
I re-examined it too closely, so I won't right now !-)
If anyone else wants it to look at or hack at, please email me at

kenr@cc.rochester.edu
--
-ken rich                   -=+!+=-                   kenr@cc.rochester.edu