PDA

View Full Version : simple perl scripts


bsdjunkie
June 2nd, 2003, 18:57
hostnames to IP

#!/usr/bin/perl
#use like test.pl < hostnames.txt

use Socket;
while (<>) {
chomp;
my $packed_address = gethostbyname($_);
unless ($packed_address) {
print "$_ => ?\n";
next;
}
my $dotted_quad = inet_ntoa($packed_address);
print "$_ => $dotted_quad\n";
}


IP to Hostnames

#!/usr/bin/perl

use Socket;
my $ADDR_PAT = '^\d+\.\d+\.\d+\.\d+$';
while (<>) {
chomp;
die "$_: Not a valid address" unless /$ADDR_PAT/o;
my $name = gethostbyaddr(inet_aton($_),AF_INET);
$name ||= '?';
print "$_ => $name\n";
}