Function with ability to resolve IP addresses and hostnames (assuming the underlying systems DNS and/or HOSTS files are properly setup) with some error checking: Valid IP address and hostname doesn't start with a number.
# Simple Resolver Function # with some error checking: # (invalid ips, host starts with a number) use Socket; # wrapper -- feed it from STDIN while (<>) { &resolve ($_); } # the actual SUB sub resolve($) { my $target = $_; chomp $target; # kill newline ARGH! I wasted hours on this!! if ($target =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/&&((1|$1|$2|$3|$4)<25 +6)) { # if $target is a valid IP address my $hostname = gethostbyaddr(inet_aton($target), AF_INET); # a +ttempt to resolve if (! $hostname) { # gethostbyaddr returned undef print "Couldn't resolve ($target)\n"; } else { print "$hostname\n"; } } elsif ($target =~ /^\d/ ){ # $target starts with a number and of c +ourse wasn't caught by above loop print "Invalid Host $target\n"; } else { # resolve an actual validish hostname my $ip = gethostbyname($target); if ($ip) { # gethostbyname actually returned something other t +han undef $ip = inet_ntoa($ip); print "$ip\n"; } else { print "Couldn't resolve ($target)\n"; } } }

Replies are listed 'Best First'.
Re: Simple Resolver Function
by jdporter (Paladin) on Mar 20, 2003 at 21:28 UTC
    You're trying to do too much of the work yourself.
    use Socket; my $address = shift or die "Usage: $0 address\n"; my $a = gethostbyname $address; $\ = $/; print defined $a ? inet_ntoa($a) : "Can't resolve '$address'";
    Sample output:
    % ./sample_resolver.pl Usage: ./sample_resolver.pl address % ./sample_resolver.pl 127.1 127.0.0.1 % ./sample_resolver.pl 127.54321 127.0.212.49 % ./sample_resolver.pl 3456776543 206.10.57.95 % ./sample_resolver.pl localhost 127.0.0.1 % ./sample_resolver.pl perlmonks.org 209.197.123.153 % ./sample_resolver.pl 040.030.020.010 32.24.16.8 % ./sample_resolver.pl foo.bar Can't resolve 'foo.bar' % ./sample_resolver.pl 255.0.1 255.0.0.1 % ./sample_resolver.pl 256.0.1 Can't resolve '256.0.1'

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

      Great - thanks! I think I understand more what the inet and gethost functions can do; however, your example doesn't do IP->Hostname lookups (reverse) which I needed in particular for my purposes. I needed to the know if there was a dotted-quad being looked up if there was no reverse for, not if it was just a valid ip address. Thanks for your input, this is a great place to throw around ideas!
Re: Simple Resolver Function
by dakkar (Hermit) on Mar 20, 2003 at 18:28 UTC

    You only resolve addresses in 4-number dotted decimal notation. What about addresses like 0177.1, better known as 127.0.0.1? OK, they are not very frequent, but still...

    Moreover, an address is numeric if its last part is numeric, not the first. That is, all TLD are non-numeric.

    To formalize:

    • an address (name or number) is made up of a number of components separated by dots
    • If the last component is a number (matches \d+), the address is numeric
    • the components of a non-numeric address match [a-zA-Z0-9-] (no, you can't have an 'underscore' in your domain names) (and this might change once non-ASCII-7 characters are allowed in names)
    • each component of a numeric address is to be interpreted as a base-16 number if it matches /^0x/i, as a base-8 number if it matches /^0/, otherwise as a base-10 number
    • a numeric address is composed of 1,2,3 or 4 components:
      • if it's one component, it must be withit the range 0...(2^32-1), and represents the entire address
      • if it's two components, the first must be in the range 0..255, and the second in 0..(2^24-1), and the address is $1<<24+$2 (I use $1, $2, and so on for the various components, left-to-right)
      • if it's three components, the first and the second must be in 0..255, and the third in 0..(2^16-1), and the address is $1<<4+$2<<16+$3
      • If it's four components, each must be in the range 0..255, and the address is $1<<24+$2<<16+$3<<8+$4

    Hope this clears up the notation.

    -- 
            dakkar - Mobilis in mobile
    
Re: Simple Resolver Function
by Aristotle (Chancellor) on Mar 22, 2003 at 13:25 UTC