CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Extracting an IPv4 address from line

 



mant1s
New User

Jul 5, 2011, 2:01 PM

Post #1 of 3 (2520 views)
Extracting an IPv4 address from line Can't Post

Hi All,

I'm wondering what I should do to extract the second IP address from a log file I have. My log file looks like:



Code
[03072011 13:32:01] connection connection.c:3880-message: connection 0x8fa0fe0 accept/tcp/none [130.55.252.94:21->184.105.172.67:34627] state: none->established


I only want to snag the address 184.105.172.67. Currently I'm using the following function to narrow it down but it doesn't seem to be working. Any pointers!?


Code
##Loop to extract the ips 
foreach(@msg3880)
{
my ($selected) = $_;
if ($selected =~ m/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/)
{
my $ip = $1 << 24 | $2 << 16 | $3 << 8 | $4;
print $ip;
}
}


Note @msg3880 is an array that holds all the lines above. Any help would be appreciated!


BillKSmith
Veteran

Jul 5, 2011, 8:53 PM

Post #2 of 3 (2510 views)
Re: [mant1s] Extracting an IPv4 address from line [In reply to] Can't Post

You are looking for the address at the start of the line. It is not there so no match. Replace "^" with "->" to look for the address immediately after the "arrow".



Perl's bit-wise OR ("|") probably does not do what you expect. Refer to the paragraph "Bitwise Or and Exclusive Or" (and its rererences) in perldoc perlop.
Good Luck,
Bill


miller
User

Jul 7, 2011, 3:07 PM

Post #3 of 3 (2460 views)
Re: [mant1s] Extracting an IPv4 address from line [In reply to] Can't Post

use Regexp::Common qw(net);

- Miller

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives