Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to gather IP address from Recevied: headers

Reply
Thread Tools

How to gather IP address from Recevied: headers

 
 
David Mahon
Guest
Posts: n/a
 
      12-03-2005
I have a perl program which examines an email (using email::simple).

One of the headers is gathered by:

@received=$mail->header("Received");


and saved using:

print OUTFILE "Received: @received\n" if @received;


How can I extract the IP address of the machine that passed the email to
one of my machines? My system has multiple MX records. I want the IP
address of the machine sending the mail to one of the ones under my
control.

In almost all of the following examples, I would be wanting
"81.187.230.6" to be returned, as this was the outgoing smtp server for
my test messages (except for the last one, which is a local message).


Here is an example of mail sent direct:

Received: from saturn.amigo.co.uk ([81.187.230.6]) by
jupiter.amigo.co.uk with esmtp (Exim 4.54) id 1EiTyM-0002I1-Lr for
http://www.velocityreviews.com/forums/(E-Mail Removed); Sat, 03 Dec 2005 09:46:26 +0000 from
earth.amigo.co.uk ( [81.187.230.3]) by amigo.co.uk with ESMTP
(Mailtraq/2.7.1.1862) id AMGC2B212AD8 for (E-Mail Removed); Sat, 03
Dec 2005 09:46:21 -0000


Here is an example by received from a secondary MX:

Received: from b.hopeless.aaisp.net.uk ([81.187.81.12]
ident=[U2FsdGVkX199l1baK8tXe/WvO+LC4Iu53KbUETkAmKk=]) by
jupiter.amigo.co.uk with esmtp (Exim 4.54) id 1EiU4f-0002Md-MH for
(E-Mail Removed); Sat, 03 Dec 2005 09:52:57 +0000 from
saturn.amigo.co.uk ([81.187.230.6]) by B.hopeless.aaisp.net.uk
([81.187.81.12]) with AAISP icebox mailer (build Nov 15 2005 17:36:31)
for (E-Mail Removed); Sat, 03 Dec 2005 09:52:57 +0000 from
earth.amigo.co.uk ( [81.187.230.3]) by amigo.co.uk with ESMTP
(Mailtraq/2.7.1.1862) id AMGC2B212AE5 for (E-Mail Removed); Sat, 03
Dec 2005 09:52:42 -0000


Here is an example received from my tertiary MX:

Received: from regardless.aaisp.net.uk ([81.187.30.42]
helo=tertiary-mx.co.uk
ident=[U2FsdGVkX185NJTsN0/QIxJfejckIpUPKbUwtfqmKjI=]) by
jupiter.amigo.co.uk with esmtps (TLS-1.0HE_RSA_AES_256_CBC_SHA1:32)
(Exim 4.54) id 1EiU3S-0002L7-A1 for (E-Mail Removed); Sat, 03 Dec
2005 09:51:42 +0000 from saturn.amigo.co.uk ([81.187.230.6]) by
tertiary-mx.co.uk with esmtp (Exim 4.43) id 1EiU3Q-0005ES-D4 for
(E-Mail Removed); Sat, 03 Dec 2005 09:51:40 +0000 from
earth.amigo.co.uk ( [81.187.230.3]) by amigo.co.uk with ESMTP
(Mailtraq/2.7.1.1862) id AMGC2B212ADD for (E-Mail Removed); Sat, 03
Dec 2005 09:49:27 -0000


(it's even possible that the mail could go tertiary -> secondary ->
primary MX)

Received: from b.hopeless.aaisp.net.uk ([81.187.81.12]
ident=[U2FsdGVkX19ybkaIvD81d8AAmAYb2rGz2ZDrfye9M/0=]) by
jupiter.amigo.co.uk with esmtp (Exim 4.54) id 1EiUFm-0002Uw-By for
(E-Mail Removed); Sat, 03 Dec 2005 10:04:26 +0000 from
regardless.aaisp.net.uk ([2001:8B0:0:30::51BB:1E2A]
helo=tertiary-mx.co.uk) by B.hopeless.aaisp.net.uk
([2001:8B0:0:81::51BB:510C]) with AAISP icebox mailer (build Nov 15 2005
17:36:31) for (E-Mail Removed); Sat, 03 Dec 2005 10:04:25 +0000 from
saturn.amigo.co.uk ([81.187.230.6]) by tertiary-mx.co.uk with esmtp
(Exim 4.43) id 1EiUFl-0005jk-Ms for (E-Mail Removed); Sat, 03 Dec
2005 10:04:25 +0000 from earth.amigo.co.uk ( [81.187.230.3]) by
amigo.co.uk with ESMTP (Mailtraq/2.7.1.1862) id AMGC2B222AF8 for
(E-Mail Removed); Sat, 03 Dec 2005 10:04:08 -0000


The final (I hope) alternative is that the mail is sent locally, and
therefore should be recognised as 127.0.0.1

Received: from user1 by jupiter.amigo.co.uk with local (Exim 4.54) id
1EfkYM-0006qz-Oo for (E-Mail Removed); Fri, 25 Nov 2005 20:52:18
+0000


I suppose I want the first IP address (address enclosed in []) that
isn't an IP address of my MX hosts. Made slightly more complex by the
fact that some use IP6 addresses on occasion.

So, I want the first IP address enclosed in [] that is NOT

81.187.81.12 or 2001:8B0:0:81::51BB:510C
81.187.30.42 or 2001:8B0:0:30::51BB:1E2A

or 127.0.0.1 if there are none.

Help please - how do I do this?



--
David Mahon
 
Reply With Quote
 
 
 
 
David Mahon
Guest
Posts: n/a
 
      12-03-2005
In article <(E-Mail Removed)>, David Mahon
<(E-Mail Removed)> writes
>I have a perl program which examines an email (using email::simple).
>
>One of the headers is gathered by:
>
>@received=$mail->header("Received");



>I suppose I want the first IP address (address enclosed in []) that
>isn't an IP address of my MX hosts. Made slightly more complex by the
>fact that some use IP6 addresses on occasion.
>
>So, I want the first IP address enclosed in [] that is NOT
>
>81.187.81.12 or 2001:8B0:0:81::51BB:510C
>81.187.30.42 or 2001:8B0:0:30::51BB:1E2A
>
>or 127.0.0.1 if there are none.
>
>Help please - how do I do this?


An alternative way may be to use the scalar method:

$received=$mail->header("Received");

to get the first received header

and test that. If it's not what I want, then get the second received
header:

$received=$mail->header("Received",2);

etc. until I get the information required.

--
David Mahon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How can I gather the user languages for IE and FireFox? ABC ASP .Net 0 12-13-2005 10:12 AM
Gather Local ComputerName Joseph ASP .Net 2 04-23-2004 05:14 PM
Gather Results From Aspx File Laidbak ASP .Net 0 01-22-2004 11:24 AM
How to gather the MAC-address Gabriel Schuster Perl Misc 2 07-06-2003 08:22 PM



Advertisments