#!/usr/bin/perl # # Script to do DNS lookup on a text file # will look up the first ip address it finds in each line # (c) Kim Holburn ANU 2004 # released under gpl http://www.gnu.org/copyleft/gpl.html # sub fail_usage { if (scalar @_) { print " \n"; print "$0: error: \n"; map { print " $_\n"; } @_ } print " \n"; print "$0: Usage: \n"; print " $0 [options] < file\n"; print " options: -d (-b)|-n|-i|-1 \n"; print " -n (domain names only) \n"; print " -i (ip numbers only) \n"; print " -1 (both but only print one) \n"; print " -b (both (default)) \n"; print " -h|--help (this screen) \n"; print " \n"; exit (scalar @_) ; } $opt=""; $debug=0; while ($ARGV=$ARGV[0]) { # if ($ARGV eq '-m') { # shift @ARGV; # if (!($ARGV = $ARGV[0])) { fail_usage ("-m needs an argument"); } # if ($ARGV =~ /^-/) { fail_usage ("-m must be followed by an argument"); } # $opt_m = $ARGV; # } if ($ARGV eq '-n' ) { if ($opt) { fail_usage ("only one of options (bni1) permitted"); } $opt = 'n'; } elsif ($ARGV eq '-i' ) { if ($opt) { fail_usage ("only one of options (bni1) permitted"); } $opt = 'i'; } elsif ($ARGV eq '-1' ) { if ($opt) { fail_usage ("only one of options (bni1) permitted"); } $opt = '1'; } elsif ($ARGV eq '-b' ) { if ($opt) { fail_usage ("only one of options (bni1) permitted"); } $opt = 'b'; } elsif ($ARGV eq '-d' ) { $debug++ ; } elsif ($ARGV eq '-h' || $ARGV eq '--help' ) { fail_usage(); } elsif ($ARGV =~ /^-/ ) { fail_usage " invalid option ($ARGV)"; } else { last ; } shift @ARGV; } if ($debug) { print "args were : "; } if (!$opt) { $opt = 'b'; } if ($debug) { print " opt=($opt) debug=($debug)"; } if ($debug) { print "\n"; } if ($debug) { print "additional arguments were: ", join (':', @ARGV), "\n"; } use Socket; while (<>) { if (!/\./) { print "skipped\n"; next; } # print "debug opt=($opt)\n"; if (($opt ne 'n') && (/([^.a-z0-9-]|^)([a-z][a-z0-9-.]*\.([a-z][a-z]|com|edu|org|gov|mil|net|biz|info|name|museum|coop|aero|pro))([^\.a-z0-9-]|$)/i)) { $ip=""; if ($debug) { print "debug looking up \"$2\" \n"; } $addr = gethostbyname($2); if (length ($addr) == 4) { $ip = inet_ntoa ($addr); if ($ip ne "" ) { if ($opt eq 'i'||$opt eq '1') { s/$2/$ip/; } } } if ($opt eq 'b') { my $old=$2; if ($ip eq "") { $ip = "not found"; } s/$old/$old\[$ip\]/; } # print "$addr\n"; } elsif (($opt ne 'i') && /([^\.0-9]|^)([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})([^\.0-9]|$)/) { if ($debug) { print "debug looking up \"$2\" \n"; } $addr = inet_aton($2); $name = gethostbyaddr($addr,AF_INET); if ($name ne "") { if ($opt eq 'd'||$opt eq '1') { s/$2/$name/; } } if ($opt eq 'b') { my $old=$2; if ($name eq "") { $name = "not found"; } s/$old/$name\[$old\]/; } } else { if ($debug) { print "nothing done\n"; } } print ; }