Home: General Discussions: General Questions:
search IP address



judeski
Deleted

Feb 27, 2001, 8:02 PM


Views: 8363
search IP address

i have this script/regex that's supposed to open a file (text or html) and search for an IP address on the page in the form of say: 209.245.56.103 and put it in a variable.

part of the script is written below. the regex may be inaccurate at this time, and the script shown is just for it to print the ip address just to confirm that i've read the IP address correctly. this is not the working version, as i still have to make a lot of changes. anyway my point is, no matter what i do, could not read the IP address in any way. what is returned is the decimal equivalent of a 32bit binary number (2 to the power of 32) which is equal to: 4294967296. any hint guys is really appreciated as i really have run out of options...

thanks in advance...

------------------------------------------------
#!/usr/bin/perl

$file = "ipaddress.html";

open(FILE,"<$file") or die "Can't open file - $file - for reading";

print "Content-type: text/html\n\n";

print "<html><body>";

if ($ipnum = ~/^\d\d\d\.\d\d\d\.\d\d\d\.\d\d\d/)
{
print "$ipnum";
print "
";
}

print "</body></html>";
close(FILE);
-------------------------------------------------



Jean
User


Feb 27, 2001, 9:53 PM


Views: 8360
Re: search IP address

If I'm right, there were several mistakes in the script.
The following version reads the file and prints the IPs located in the beginning of each line.
Hope this helps ...



Code
#!/usr/bin/perl 
$file = "ipaddress.html";
open(FILE,"<$file") or die "Can't open file - $file - for reading";
print "Content-type: text/html\n\n";
print "<html><body>";
# Loops through all the lines of the $file
while ($line = <FILE>) {
# Look for the IP in the beginning of each line
# Spaces are allowed before the actual IP entry (\s*)
if ($ipnum =~ /^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
$ipnum = $1; # Assigns the IP found in the previous line.
print "$ipnum\n"; # Prints the IP and the new-line character(s).
}
}
print "</body></html>";
close(FILE);

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


judeski
Deleted

Mar 1, 2001, 4:47 PM


Views: 8346
Re: search IP address

thanks a lot Jean, the script does read the file now, and i don't get that strange number anymore. however, i still don't get a match on the IP address. help! before i lose all my hair !

thanks again in advance...



Jean
User


Mar 4, 2001, 12:09 AM


Views: 8338
Re: search IP address

The regex that I've used in the example looks for the IP in the beginning of a line contained in $ipnum (not in the $line that contains the lines read from the file). I've left this because you said the code posted was just a part of larger program and it didn't show the contents of the $ipnum... You could use the following examples in order to change the behaviour of the example from my previous post:

1. $ipnum =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
Looks for an IP in all the $ipnum, not just in the beginning;
2. $line =~ /^\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
Looks for an IP in the beginning if the $line that is,
in the beginning of each line of the file being read;
3. $line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
Looks for an IP in all the $line that is,
IP will be found if it's contained ANYWHERE in the file;


Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


judeski
Deleted

Mar 7, 2001, 10:14 AM


Views: 8332
Re: search IP address

awesome ! thanks again...