Perl脚本,用于从邮件头解析发件人的原始IP

时间:2015-12-06 20:36:03

标签: linux perl email parsing email-headers

我正在寻找一个perl脚本(从邮件头解析发件人的原始IP),我不知道该怎么做......

在我看了谷歌后,我发现我必须使用一些Perl模块,如:Mail :: Field,Mail :: Header,Email :: Simple等。

不幸的是,大多数例子都集中在发件人,收件人或/和主题上。少关于发件人的原始IP。

欢迎任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

不确定这是你想要的:

假设:你有一个hend txt文件 从同一行的ipaddress收到

运行:sender_ip.pl header.txt

[gliang @ www perl_tools] $ cat sender_ip.pl

#!/usr/bin/perl
use strict;
use warnings;

return 1  unless $0 eq __FILE__;
main(@ARGV) if $0 eq __FILE__;

sub main{
    my @parameters = @_;

    my $file = check_input(@parameters);
    my @headerlines = read_header($file);
    my @ips  = get_ips(@headerlines);

    if(scalar(@ips)>0){
        print "\n*** sender ips @ips\n";
    }else{
        print "\n\n*** did not find sender ip\n";
    }
}

sub check_input{
    my @parameters  = @_;

    if(scalar(@parameters)>0){

    }else{
        print "\n*** add header.txt file, please!\n";
        exit(1);
    }
    my $file = $parameters[0];
    if(-e $file){

    }else{
        print "\n*** input file does not exist\n\n";
        exit(1);
    }
    return $file;
}

sub read_header{
    my $file  = $_[0];
    open(my $FILE, '<', $file);
    my @headerlines = <$FILE>;
    close($FILE);
    return @headerlines;
}


sub get_ips{
    my @headerlines= @_;

    my @ips;
    foreach my $line(@headerlines){
        chomp($line);
        if($line =~ m/Received/ && $line=~ m/from/i){
            if ($line =~ /(\d+\.\d+\.\d+\.\d+)/){
                my $ip = $1;
                push(@ips, $ip);
            }
        }
    }
    return @ips;
}