弁財天

ゴフマン「専門家を信じるのではなく、自分自身で考えて判断せよ」

Maildir下のファイルからwhitelistをバッチ作成

How to use Email::Address to extract emails from a file with perl

#!/usr/bin/perl

use warnings;
use strict;

use Email::Address;

open(my $fh, '<', '/var/log/maillog')
    or die "Cannot open /var/log/maillog: $!";

while (<$fh>) {
  next unless /msgif/;

  my @addrs = Email::Address->parse($_);

  foreach my $addr (@addrs) {
    print "$addr\n";
  }
}

close($fh);

Perl extract domain name from email address inc tld but excluding subdomains

use Email::Address;
my ($addr) = Email::Address->parse('foo@domain.co.uk');
print "Domain: ".$addr->host."\n";
print "User:   ".$addr->user."\n";

#!/usr/bin/perl

use strict;
use warnings;

use Email::Address;

my $MAILDIR = '/home/hoge/Maildir';

my %DOMAINS;

sub extract_mailaddr() {
        my ($file) = @_;

        #print "$file ...\n";
        open(my $fh, '<', $file) or return;

        while (<$fh>) {
                next unless /^From/;
                my @addrs = Email::Address->parse($_);

                foreach my $addr (@addrs) {
                        my ($parse_addr) = Email::Address->parse($addr);
                        $DOMAINS{$addr->host} = 1;
                        last;
                }
        }
        close($fh);
}

sub read_cur_dir() {
        my ($dir) = @_;
        print STDERR "$dir ...\n";
        opendir (D, "$MAILDIR/$dir/cur") or return;
        while (my $file = readdir(D)) {
                &extract_mailaddr("$MAILDIR/$dir/cur/$file");
        }
        closedir(D);
}

opendir (DIR, $MAILDIR) or die $!;
while (my $file = readdir(DIR)) {
        if ($file =~ /.INBOX.Spam/) {
                print STDERR "Skip $file ...\n";
                next;
        }
        if (-d "$MAILDIR/$file" && $file =~ /.INBOX/) {
                &read_cur_dir($file);
        }
}

closedir(DIR);

foreach my $domain (keys(%DOMAINS)) {
       print $domain . "\n";
}

Sort a list of domain names (FQDNs) starting from tld and working left

#!/usr/bin/python

from fileinput import input
for y in sorted([x.strip().split('.')[::-1] for x in input()]): print '.'.join(y[::-1])

投稿されたコメント:

コメント
コメントは無効になっています。