Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Extract IP addresses

by reisinge (Hermit)
on Jan 29, 2021 at 08:14 UTC ( [id://11127622]=CUFP: print w/replies, xml ) Need Help??

In my blog post I used a Perl one-liner to extract IP addresses from logs:

journalctl --since "00:00" | perl -lne '/((?:\d{1,3}\.){3}\d{1,3})/ & +& print $1'
A tiny code but pretty useful.
Noi siamo quello che facciamo. -- L. Sciascia

Replies are listed 'Best First'.
Re: Extract IP addresses
by Discipulus (Canon) on Jan 29, 2021 at 09:20 UTC
    Hello reisinge,

    you may be interested in Regexp::Common too to cover edge cases:

    perl -MRegexp::Common=net -E "say +(/$RE{net}{IPv4}/?'IP ':'NOT ip '). +$_ for @ARGV" 10.10.10.1 888.888.888.888 8.8.8.8 0.0.0.0 IP 10.10.10.1 NOT ip 888.888.888.888 IP 8.8.8.8 IP 0.0.0.0

    L*

    PS: my congratulations for your signature!

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Extract IP addresses
by AnomalousMonk (Archbishop) on Jan 30, 2021 at 03:48 UTC

    Further to Discipulus's post:     Note that $RE{net}{IPv4} from Regexp::Common (in common with many patterns in this family of modules) intentionally has no boundary assertions; these are left to the programmer and are determined by the exact end-use of the pattern. So 999.9.9.999 will have 99.9.9.99 extracted from it as a valid IP if no bounds are specified.

    Were one extracting IPs from a string, one might do something like this:

    Win8 Strawberry 5.30.3.1 (64) Fri 01/29/2021 22:12:11 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Regexp::Common; my $rx_extract_ipv4 = qr{ (?<! \d) $RE{net}{IPv4} (?! \d) }xms; for my $s ( '1.2.3.4foo11.22.33.44bar111.222.233.244 0.0.0.0 .255.255.255.255. +', '999.9.9.999 256.255.255.256', ) { my @ips = $s =~ m{ $rx_extract_ipv4 }xmsg; printf "'$s' \n -> (%s) \n\n", join ' ', map "'$_'", @ips; } ^Z '1.2.3.4foo11.22.33.44bar111.222.233.244 0.0.0.0 .255.255.255.255.' -> ('1.2.3.4' '11.22.33.44' '111.222.233.244' '0.0.0.0' '255.255.255 +.255') '999.9.9.999 256.255.255.256' -> ()
    A roll-your-own approach to extracting decimal octet IPv4s might be:
    Win8 Strawberry 5.8.9.5 (32) Fri 01/29/2021 22:22:11 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $rx_dec_octet = qr{ 25[0-5] | 2[0-4]\d | [01]? \d \d? }xms +; my $rx_ipv4 = qr{ $rx_dec_octet (?: [.] $rx_dec_octet){3} }xms +; my $rx_extract_ipv4 = qr{ (?<! \d) $rx_ipv4 (?! \d) }xms +; for my $s ( '1.2.3.4foo11.22.33.44bar111.222.233.244 0.0.0.0 .255.255.255.255. +', '999.9.9.999 256.255.255.256', ) { my @ips = $s =~ m{ $rx_extract_ipv4 }xmsg; printf "'$s' \n -> (%s) \n\n", join ' ', map "'$_'", @ips; } ^Z '1.2.3.4foo11.22.33.44bar111.222.233.244 0.0.0.0 .255.255.255.255.' -> ('1.2.3.4' '11.22.33.44' '111.222.233.244' '0.0.0.0' '255.255.255 +.255') '999.9.9.999 256.255.255.256' -> ()
    Of course, it's up to the programmer to chose boundary assertions appropriate to the string extraction context. (And lots of testing is a good idea, too. :)

    Update: Minor wording changes.


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://11127622]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found