#!/perl/bin/perl

# lookup.pl
# A little Internet domain name and IP address resolver.
# To be run from the command line.

    $AF_INET = 2;        # Always this value.

# Get name of address to resolve from the command line.

    $Address = $ARGV[0];

# Treat the command line argument as an IP address by attempting
# to turn whatever it is into a four-byte numeric value.

    @IP = split (/\./, $Address);
    $IPAddress = pack ("C4", @IP);

# Try gethostbyaddr() first.

    ($name, $aliases, $type, $length, $addr)
        = gethostbyaddr ($IPAddress, $AF_INET);

# If $name comes back blank, it's a good indication that
# gethostbyaddr() failed. Try gethostbyname() with the
# original command line argument.

    if ($name eq "")
        {
        ($name, $aliases, $type, $length, $addr)
            = gethostbyname ($Address);
        }

# Turn the value in $addr into a string IP address.

    @Addrs = unpack ("C4", $addr);
    $StringIP = join (".", @Addrs);

# If the name is still empty, or if gethostbyname() has returned
# a name that is the same as the unpacked $addr, the lookup probably
# failed. Otherwise, print the name and IP address.

    if (($name eq "") || ($name eq $StringIP))
        {
        print "Unable to resolve $Address\n";
        }
    else
        {
        print "$name\n";
        print "$StringIP\n";
        }

#                    End lookup.pl

