email parsing->mysql:: Can someone explain me this perl script?

Discussion in 'Perl Misc' started by albatroz, Aug 30, 2004.

  1. albatroz

    albatroz Guest

    I am a perl noob that can learn if you give me a hand.
    I currently need a script to read emails using email piping,
    extract some information and put it inside a MySQL database.

    The reason? Make some statistics of my email server antivirus.

    The sample body of my emails is as follows:

    -----------------> Source of Sample email source <---------

    From: "MailScanner" <>
    To:
    Subject: Advertencia: Virus detectado en e-mail
    Message-Id: <>
    Date: Sun, 29 Aug 2004 17:33:08 -0500
    Body:
    Se han encontrado virus en el siguiente e-mail:
    Sender:
    IP Address: 200.106.12.141.22214
    Recipient:
    Subject: RETURNED MAIL: SEE TRANSCRIPT FOR DETAILS
    MessageID: 1C1YEL-0002Zn-VA
    Informe: message.zip contains Worm.Mydoom.M
    -----------------> Source of Sample email source <---------

    For this purpose I got this script (called amavistat), however
    I need that someone tells me what modifications should I do to
    to make it work in my case, here I am attaching the source of
    the script

    #!/usr/bin/perl
    # Veghead 2001
    # Fixed stupid '=' vs 'eq' bug - Veghead 2003-08-14
    #
    # changes 18.08.03 - Marcus Schopen <>

    use DBD::mysql;
    $server='localhost';
    $dbname='amavistat';
    $table='virus';
    $username='xxx';
    $password='xxx';

    $state="inheader";
    $dbh=DBI->connect("DBI:mysql:$dbname:$server",$username,$password) or
    die("Can't connect to MySQL");

    $state="inheader";

    while(<>) {
    chomp;
    if ($state eq "inheader") {
    if (length($_)<2) {$state="inbody";}
    elsif ($_=~/^Subject: VIRUS FROM <(.*)> \((.*)\)/) {
    $virusfrom=$1;
    $viruses=$2;
    $viruswarn++;
    }
    } elsif ($state="inbody") {
    if ($_=~/^-> <([^\@]+\@[^\@]+)>/) {
    $virusto=$1;
    if ($viruswarn) {do_something();}
    }
    }
    }

    sub do_something {
    foreach $vname ($viruses) {
    print "From: $virusfrom\nTo: $virusto\nVirus: $vname\n\n";
    $sth = $dbh->prepare("insert into $table values
    (0,now(),'$virusfrom','$virusto',
    '$vname')");
    $sth->execute;
    $sth->finish;
    }
    }
     
    albatroz, Aug 30, 2004
    #1
    1. Advertising

  2. albatroz <> wrote:


    > For this purpose I got this script (called amavistat), however
    > I need that someone

    ^^^^^^^
    ^^^^^^^
    > # Veghead 2001
    > # changes 18.08.03 - Marcus Schopen <>



    Ask Veghead or Marcus, it is their (amateurish looking) program.

    If they won't help, then you will need to learn some Perl
    before you will be able to modify the Perl program.


    What part of the program you posted are you having trouble figuring out?

    If you tell us where you are stuck, we can probably help unstick you.

    If you don't understand any of the parts, then go off and learn
    Perl until you do, then come back here with any remaining questions.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 31, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    0
    Views:
    1,863
  2. suzy
    Replies:
    3
    Views:
    460
  3. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    485
    BluDog
    Oct 5, 2004
  4. Karl Seguin

    Re: Can someone explain this?

    Karl Seguin, Dec 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    298
    John Saunders
    Dec 13, 2004
  5. Shapper
    Replies:
    3
    Views:
    421
    willow
    Jun 10, 2005
Loading...

Share This Page