Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl IP address search 1

Status
Not open for further replies.

judeski

Vendor
Oct 17, 2001
2
US
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,&quot;<$file&quot;) or die &quot;Can't open file - $file - for reading&quot;;

print &quot;Content-type: text/html\n\n&quot;;

print &quot;<html><body>&quot;;

if ($ipnum = ~/^\d\d\d\.\d\d\d\.\d\d\d\.\d\d\d/)
{
print &quot;$ipnum&quot;;
print &quot;<br>&quot;;
}

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

 
Uh, your script is a little incomplete (where do you read the file in?), but this regex works to catch the basic form of an ip addy:[tt]

if ( $filedata =~ m~((\d{1,3}\.){3}\d{1,3})~ )
{
print $1;
}[/tt]

but only once. if you have more than one ip in a file, you'll need a global search. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
the reason you're getting that huge number is because there's a space between your '=' and '~'. to do a regex match, there's no space. what you're doing is a bitwise operation, a complement to be exact. try typing on the command line:[tt]
perl -e &quot;print ~1;&quot;[/tt]

and you should get the exact same number. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top