Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Perl IP's etc


On Sun, Sep 16, 2001 at 11:39:33PM +1000, peterw wrote:

> Given an alpha numeric string of vaying length that contains 0 or more
> valid IP numbers(dotted quad decimal)and each IP(if there is one) is
> seperated form the next IP or other text by one or more spaces how can I
> use Perl to retrieve thes IP's  into an array ?

the following lil script will print out every single IP address found in a file:

    #!/usr/local/bin/perl -w

    $ip_regex = qr/([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\.
                   ([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])/x;

    while (<>) {
        while (/$ip_regex/g) {
            print "found IP $1.$2.$3.$4\n";
        }
    }

adjust as needed.


-- 
#ozone/algorithm <ozone@xxxxxxxxxxxxxxxx>          - trust.in.love.to.save