1

I am trying to extract all of the IP Addresses off of this website: http://www.game-monitor.com/

I want to regex the IP's on that page, extract all of them and display them on the screen.

This is what I have so far, can you tell me what Is wrong and help me?

#!/usr/bin/perl

use HTTP::Request;
use LWP::UserAgent;

print 'Press [1] To Begin: ';
chomp ($begin = <STDIN>);

my $url = 'http://www.game-monitor.com/';
my @ips = ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}','\d{1,3}\.\d{1,2}\.\d{1,3}\.\d{1,2}','\d{1,2}   \.\d{1,3}\.\d{1,2}\.\d{1,3}','\d{1,2}\.\d{1,2}\.\d{1,2}\.\d{1,3}','\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,2}','\d{1,3}\.\d{1,3}\.\d{1,2}\.\d{1,2}','\d{1,2}\.\d{1,2}\.\d{1,3}\.\d{1,3}','\d{1,2}\.\d{1,2}\.\d{1,2}\.\d{1,2}','\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,2}','\d{1,3}\.\d{1,2}\.\d{1,2}\.\d{1,3}');

if ($begin eq 1)
{
my $request = HTTP::Request->new(GET => $url);
my $useragent = LWP::UserAgent->new();
my $response = $useragent->request($request);
my $result = $response->content;

foreach $ip (@ips)
{
if ($result =~ /($ips[0])/ ||
$result =~ /($ips[1])/ ||
$result =~ /($ips[2])/ ||
$result =~ /($ips[3])/ ||
$result =~ /($ips[4])/ ||
$result =~ /($ips[5])/ ||
$result =~ /($ips[6])/ ||
$result =~ /($ips[7])/ ||
$result =~ /($ips[8])/ ||
$result =~ /($ips[9])/
)
{
    print "IP: $1 \n";
    print "IP: $2 \n";
    print "IP: $3 \n";
    print "IP: $4 \n";
    print "IP: $5 \n";
    print "IP: $6 \n";
    print "IP: $7 \n";
    print "IP: $8 \n";
    print "IP: $9 \n";
    print "IP: $10 \n";
}
}
}
1
  • what problem are you having? what error are you getting? Commented May 25, 2011 at 7:31

4 Answers 4

3

To simplify multi-line substitutions, use the /s modifier, which in effect tells Perl to pretend the string is a single line--even if it isn't.

see perlre for more detail.

It would be nice if you use module like Regexp::Common::net -- provide regexes for IPv4 addresses instead of writing your own regex for matching ip addresses.

for example try something like,

use Regexp::Common qw/net/;
while (<>) {
  print $1, "\n" if /($RE{net}{ipv4})/;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't see what the s modifier is good for in this case. g is needed here to match all occurrences.
3

Use the /g modifier to match all IPs. Tip: use -w parameter and strict package to avoid "bad coding style".

#!/usr/bin/perl -w

use strict;
use HTTP::Request;
use LWP::UserAgent;

print 'Press [1] To Begin: ';
chomp (my $begin = <STDIN>);

my $url = 'http://www.game-monitor.com/';
my $ip_regex = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if ($begin eq 1)
{
    my $request = HTTP::Request->new(GET => $url);
    my $useragent = LWP::UserAgent->new();
    my $response = $useragent->request($request);
    my $result = $response->content;

    while ($result =~ /($ip_regex)/g)
    {
        print "IP: $1 \n";
    }

}

5 Comments

I executed this code, but no result? Does it take a while to extract the Ip's?
For me it worked ... I get approx. 100 IP addresses in return. Do you have any error messages?
Hm, that's odd. I don't get any error messages, just a ticker flashing constantly not displaying anything.
How do you run your code? I do it on Linux shell perl ip.pl
Try with removed if ($begin eq 1) line.
2
#!/usr/bin/perl

use HTTP::Request;
use LWP::UserAgent;


my $url = 'http://www.game-monitor.com/';
my $request = HTTP::Request->new(GET => $url);
my $useragent = LWP::UserAgent->new();
my $response = $useragent->request($request);
my $result = $response->content;

@m = ($result =~ /\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/sg);
foreach (@m) {
        print "IP: $_\n";
}

2 Comments

I executed this code, but no result? Does it take a while to extract the Ip's?
It works perfectly for me. May be the remote website is slow.
1

I don't really see what you're trying to do with your big array @ips. The first regex already matches all IP addresses (since \d{1,3} means "one to three digits", it already contains IP addresses that have two digits), so you don't need all those permutations with \d{1,2}.

One thing you could do is to surround your regex with \b word boundary anchors to ensure that you don't match 123.123.123.123 within 99123.123.123.12399 or something like it. Also, you're probably aware that your regex would also match something like 999.999.999.999. If that's not a problem because your input won't contain invalid IP addresses, then of course that's just fine.

Finally, you need the /g global modifier so your regex finds not just the first but all occurrences in the string.

In essence, how about doing it like this:

while ($result =~ m/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g) {
    print "IP: $&\n";
}

4 Comments

I executed this code, but no result? Does it take a while to extract the Ip's?
Can you post (an excerpt of) $result in your question so we can see what it contains?
When I said 'no result' I meant nothing shows up on the screen, just a ticker flashing; nothing is wrong with $result lol.
I understand, but I'd like to see what's in $result so I can reproduce why my regex wouldn't match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.