Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Extracting IP addresses from a file...

by Brian Matchick (Initiate)
on Jul 04, 2001 at 23:34 UTC ( #93921=perlquestion: print w/ replies, xml ) Need Help??
Brian Matchick has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to automate our net abuse report and am having a tough time extracting a list of IP addresses from a file. All I want to do is go though the file and build a list of every IP address contained within. I've worked out the regex to match lines containing ip addresses, but am having some trouble finding a way to actually obtain the IP numbers that it matches. The $1, $2 business is turning out to be a bit confusing. Can anyone help me out with a code example? Thanks a lot! Brian bmatchick@mindspring.com

Comment on Extracting IP addresses from a file...
Re: Extracting IP addresses from a file...
by damian1301 (Curate) on Jul 04, 2001 at 23:38 UTC
    foreach($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $1; }
    Should do it. (or some re like that.)

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.

      You can't use foreach like this - you need to use while to make your code work. See the code below for a demo:

      $data = <<'DATA'; foo 1.1.1.1 foo bar 22.22.22.22 bar baz 333.333.333.333 baz DATA # this is *wrong* foreach($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $1; } print "Foreach fails and gives:\n"; print "$_\n" for @ips; @ips = (); # this is right while($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $1; } print "While works and gives:\n"; print "$_\n" for @ips;

      For an explanation of why the for loop generates three copies of the last IP address you need to consider what has happened. A /g type regex will return an array of matches. So as it is called in array context it returns an array (this actually contains the three addresses) however $1 now contains the last address matched as we had to match all occurences to generate the array. We then iterate over this array and push $1 (the last IP address) into our array three times. To fix this we either use while or push $_ (not $1) into our IP array like this:

      $data = <<'DATA'; foo 1.1.1.1 foo bar 22.22.22.22 bar baz 333.333.333.333 baz DATA foreach($data=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g){ push @ips, $_; } print "Foreach now gives:\n"; print "$_\n" for @ips;

      Oh you can slurp up the file into a variable $data like this:

      open FILE, "<path/to/file" or die "Oops, Perl says: $!"; { local $/; $data = <FILE>; } close FILE;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Extracting IP addresses from a file...
by rrwo (Friar) on Jul 04, 2001 at 23:43 UTC

    Can you be more specific about this file? Is it a web server log? They are usually standard formats and easy to parse, often separated by a spaces.... Usually the IP address is a separate field.

    Also, what regular expression are you using?

Re: Extracting IP addresses from a file...
by the_slycer (Chaplain) on Jul 04, 2001 at 23:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://93921]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2015-07-05 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls