Ben,

>   I keep meaning to look for/write a utility to provide a command-line
> front-end to the gethostbyname().  (Yes, I know about dig(1), nslookup(8), and
> host(1) -- those all make wonderful DNS clients, but they deliberately bypass
> gethostbyname(), so you can test your DNS servers directly.  But there are
> times where I want to test what the *client* thinks is going on.)

Here's a script I wrote some time ago that does this. Comes in handy
for exactly the reason you point out! Also does gethostbyaddr for numeric.

Karl



---- cut ---- cut ---- cut ---- cut ---- cut -----
  #!/bin/sh -- # A comment mentioning perl
eval 'exec perl -S $0 ${1+"$@"}'
        if 0;

chop($Program = `basename $0`);

$Usage = <<"END";

$Program: call gethostbyname or gethostbyaddr on a list of hosts.

Usage: $Program [<host> ...]

Notes:
        numeric host => gethostbyaddr(), otherwise gethostbyname().

        if no arguments on cmd line then hosts are read from stdin, 1 per line.
        if <host> is a file, hosts are read from it, 1 per line.
END

select(STDERR); $|=1;
select(STDOUT); $|=1;

if ( $ARGV[0] =~ /^-h/ ) {
        print $Usage;
        exit 0;
} elsif ( ! @ARGV ) {
        # hosts in STDIN:
        while (<>) {
                chomp; &lookup($_);
        }
} else {
        # items in @ARGV:
        while ($host = shift) {
                if ( -f $host ) {
                        # read hosts from file:
                        if ( ! open(FILE, "<$host") ) {
                                print STDERR "could not open: $host: $!\n";
                                next;
                        }
                        while (<FILE>) {
                                chomp; &lookup($_);
                        }
                        close(FILE);
                } else {
                        # straight lookup:
                        &lookup($host);
                }
        }
}

sub lookup {
        my($host) = @_;
        return if $host =~ /^\s*$/;

        my($name, $aliases, $type, $len, @addrs, $addr);
        if ( $host =~ /^[\d\.]+$/ ) {
                # numeric addr (123.45.67.89)
                my $af = 2;
                my $ip = pack('C4', split(/\./, $host));
                ($name,$aliases,$type,$len,@addrs) = gethostbyaddr($ip, $af);
        } else {
                # hostname (foo.bar.com)
                ($name,$aliases,$type,$len,@addrs) = gethostbyname($host);
        }
        print STDOUT "host: $host\n";
        foreach $addr (@addrs) {
                my $a = join('.', unpack('C4', $addr)); 
                print STDOUT "\taddr:\t$a\n";
        }
        print STDOUT "\tname:\t$name\n";
        print STDOUT "\talii:\t$aliases\n";
        # print STDOUT "\ttype:\t$type\n";
}


**********************************************************
To unsubscribe from this list, send mail to
[EMAIL PROTECTED] with the following text in the
*body* (*not* the subject line) of the letter:
unsubscribe gnhlug
**********************************************************

Reply via email to