#!/usr/local/bin/perl # # ip2hfile.pl - 19/Aug/1995 # # by David Efflandt # # Inputs IP address from log file and outputs # host name to screen and file. # # Typical usage: ip2hfile.pl infile [outfile] # # Will prompt for filenames if not included. # $iplog = $ARGV[0]; if ($iplog eq "") { print "File to get IP's from? "; $iplog = ; chop($iplog); } $hostlog = $ARGV[1]; if ($hostlog eq "") { print "Enter file for output (screen display if none): "; $hostlog = ; chop($hostlog); } open(IP, $iplog) || warn "Can't open input $iplog: $!\n"; open(HOST, ">$hostlog") || warn "Can't open output $hostlog: $!\n"; while() { chop; @foo = split(/ /,$_); @ip = grep(/\d+\.\d+\.\d+\.\d+/,@foo); $ip = @ip[0]; @date = grep(/\d+\/\w+\/\d+/,@foo); $date = @date[0]; $date =~ s/\W*//; if ($ip) { ++$hits; if ($ip ne $dup) { ++$hosts; if ($date) { print "Date: $date\t"; print HOST "$date\t"; } print "IP: $ip\n"; print HOST "$ip\n"; @numbers = split(/\./, $ip); $ip_number = pack("C4", @numbers); ($name) = (gethostbyaddr($ip_number, 2))[0]; if ($name) { print "The host is: $name\n\n"; print HOST "$name\n\n"; } else { print "This IP has no name\n\n"; print HOST "(no host name)\n\n"; } } $dup = $ip; } } print "There were $hits file hits from $hosts hosts.\n"; print HOST "There were $hits file hits from $hosts hosts.\n"; if ($hostlog) { print "Hosts saved to file: $hostlog\n\n"; } else { print "Results were not saved: $!\n\n"; } close IP; close HOST; end;