M
Maynard
Hello all,
I'm attempting to take a log file with several thousand IP addresses
and convert them into their DNS named equivalent -- Not so hard. But
what's stumping me is some basic regex syntax. I know that the 'g'
operator should allow me to continue from where I left off, but I seem
to be missing the correct way to implement this... my script is
picking up the first IP address in the line, but not the second.
Perhaps someone can show me the error of my ways and get me back on
track.
Input log file is formatted like this:
TCP out 65.198.222.70:43679 in 192.168.0.3:80 idle 0:00:08
TCP out 209.73.24.2:29042 in 192.168.0.3:80 idle 0:00:08
TCP out 65.198.222.70:43685 in 192.168.0.3:80 idle 0:00:00
....
Complete script:
----------------------------------
use Socket;
my ($ip, $hostname);
my ($SourceFile, $DestFile) = ("log1.txt", "log2.txt");
open(INPUTFILE, "<$SourceFile") || die("ERROR: Cannot open
$SourceFile, $!");
open(OUTPUTFILE, ">$DestFile") || die("ERROR: Cannot open $DestFile,
$!");
binmode INPUTFILE;
binmode OUTPUTFILE;
while(<INPUTFILE>) {
my $line = $_;
if ($line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g) {
$ip = $1;
$hostname = lc gethostbyaddr(inet_aton($ip),AF_INET);
if ($hostname ne "") {
$line =~ s/$g_ip/$g_hostname/;
print " IP address $ip = $hostname\n";
}
else {
print " IP address $ip = *NO DNS RECORD FOUND*\n";
}
}
print OUTPUTFILE $line;
}
close(INPUTFILE);
close(OUTPUTFILE);
I'm attempting to take a log file with several thousand IP addresses
and convert them into their DNS named equivalent -- Not so hard. But
what's stumping me is some basic regex syntax. I know that the 'g'
operator should allow me to continue from where I left off, but I seem
to be missing the correct way to implement this... my script is
picking up the first IP address in the line, but not the second.
Perhaps someone can show me the error of my ways and get me back on
track.
Input log file is formatted like this:
TCP out 65.198.222.70:43679 in 192.168.0.3:80 idle 0:00:08
TCP out 209.73.24.2:29042 in 192.168.0.3:80 idle 0:00:08
TCP out 65.198.222.70:43685 in 192.168.0.3:80 idle 0:00:00
....
Complete script:
----------------------------------
use Socket;
my ($ip, $hostname);
my ($SourceFile, $DestFile) = ("log1.txt", "log2.txt");
open(INPUTFILE, "<$SourceFile") || die("ERROR: Cannot open
$SourceFile, $!");
open(OUTPUTFILE, ">$DestFile") || die("ERROR: Cannot open $DestFile,
$!");
binmode INPUTFILE;
binmode OUTPUTFILE;
while(<INPUTFILE>) {
my $line = $_;
if ($line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g) {
$ip = $1;
$hostname = lc gethostbyaddr(inet_aton($ip),AF_INET);
if ($hostname ne "") {
$line =~ s/$g_ip/$g_hostname/;
print " IP address $ip = $hostname\n";
}
else {
print " IP address $ip = *NO DNS RECORD FOUND*\n";
}
}
print OUTPUTFILE $line;
}
close(INPUTFILE);
close(OUTPUTFILE);