# 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"; } } }
In reply to Simple Resolver Function by jbaribeault
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |