in reply to Re: Extract IP from email dataset?
in thread Extract IP from email dataset?

The email format Received: (qmail 23153 invoked from network); 14 Apr 2004 21:19:04 -0000 Received: from dev213.omgkitty.com (HELO omgkitty.com) (@192.168.2.118) by dev50.omgkitty.com with SMTP; 11 Jan 2003 22:45:04 -0000 Received: (qmail 11105 invoked by uid 99); 14 Apr 2004 22:19:04 -0000 Received: (qmail 14227 invoked from network); 14 Apr 2004 22:19:04 -0000 Received: from unknown (HELO ns.eisnoc1.net) (66.54.218.128) by www.omgkitty.com with SMTP; 14 Apr 2004 22:45:04 -0000 Received: from listmail (216.157.143.2) by ns.eisnoc1.net (8.11.1/8.11.1) with ESMTP id h0BMg4000951 for <nospam@omgkitty.com>; Sat, 11 Jan 2003 17:42:04 -0500 Message-Id: <200301112242.h0BMg4000951@ns.eisnoc1.net> Date: Sat, 11 Jan 2003 15:40:09 -0500 From: "Ronald George" <ron@247customer.com> To: "Omgkitty User" <nospam@omgkitty.com> Subject: Whats up? MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Mailer: ListMail v1.65 X-LM-Flags: 1.45.82 What’s up?

Replies are listed 'Best First'.
Re^3: Extract IP from email dataset?
by CountZero (Bishop) on May 12, 2009 at 18:36 UTC
    use strict; use Regexp::Common qw /net/; my $email = q|Received: (qmail 23153 invoked from network); 14 Apr 200 +4 21:19:04 -0000 Received: from dev213.omgkitty.com (HELO omgkitty.co +m) (@192.168.2.118) by dev50.omgkitty.com with SMTP; 11 Jan 2003 22:4 +5:04 -0000 Received: (qmail 11105 invoked by uid 99); 14 Apr 2004 22: +19:04 -0000 Received: (qmail 14227 invoked from network); 14 Apr 2004 + 22:19:04 -0000 Received: from unknown (HELO ns.eisnoc1.net) (66.54.2 +18.128) by www.omgkitty.com with SMTP; 14 Apr 2004 22:45:04 -0000 Rec +eived: from listmail (216.157.143.2) by ns.eisnoc1.net (8.11.1/8.11.1 +) with ESMTP id h0BMg4000951 for <nospam@omgkitty.com>; Sat, 11 Jan 2 +003 17:42:04 -0500 Message-Id: <200301112242.h0BMg4000951@ns.eisnoc1. +net> Date: Sat, 11 Jan 2003 15:40:09 -0500 From: "Ronald George" <ron +@247customer.com> To: "Omgkitty User" <nospam@omgkitty.com> Subject: +Whats up? MIME-Version: 1.0 Content-Type: text/plain; charset="iso-88 +59-1" Content-Transfer-Encoding: 7bit X-Mailer: ListMail v1.65 X-LM-F +lags: 1.45.82 What’s up? |; while ($email =~ m/$RE{net}{IPv4}{dec}{-keep}/g) { print "$1\n"; }
    Output:
    192.168.2.118 66.54.218.128 216.157.143.2

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^3: Extract IP from email dataset?
by tweetiepooh (Hermit) on May 12, 2009 at 15:57 UTC
    If you have this "limited" set then you can simplify your regex to extract something that looks like an IP address though it won't validate it in any form.

    Just think about what you are looking for and work from there. How do you as a human pick out IP addresses? Now how can you define that in a regex?

    Don't worry if it's a valid IP since you maybe able to assume that the source already has done some of that for you. If you do want to make sure you only get valid IP then as already suggested, check CPAN. (Or work it out for yourself as a nice exercise.)

Re^3: Extract IP from email dataset?
by John M. Dlugosz (Monsignor) on May 12, 2009 at 18:28 UTC
    Please edit your post and put in <code> tags. You know the "preview" you get before committing your post? That's how it's going to look to everyone else!