Solved

Perl - Create HTML Table

Posted on 2013-12-23
3
312 Views
Last Modified: 2014-01-08
I have text files which has sample output as given below  and i am sending this emails in the given way below also  and the result on email is really bad seems as given below also


i want to organize the file output as how should i do it ?


required output of text lines :
<table>
<tr><td>timestampfromfile</td><td>srcIP</td><td>dstIP</td><td>prot</td><td>srcPort </td><td>dstPort</td><td>octets</td><td>packets</tr>
<tr><td>2013-12-23  12:06:53.776</td><td>205.196.121.163</td><td>178.20.224.151</td><td>6</td><td>80</td><td>64450</td><td>1552</td><td>256</tr>
<tr><td>2013-12-23  12:06:53.776</td><td>205.196.121.163</td><td>178.20.224.151</td><td>6</td><td>80</td><td>64450</td><td>1552</td><td>256</tr>
<tr><td>2013-12-23  12:06:53.776</td><td>205.196.121.163</td><td>178.20.224.151</td><td>6</td><td>80</td><td>64450</td><td>1552</td><td>256</tr>
<tr><td>2013-12-23  12:06:53.776</td><td>205.196.121.163</td><td>178.20.224.151</td><td>6</td><td>80</td><td>64450</td><td>1552</td><td>256</tr>
<tr><td>2013-12-23  12:06:53.776</td><td>205.196.121.163</td><td>178.20.224.151</td><td>6</td><td>80</td><td>64450</td><td>1552</td><td>256</tr><tr><td>2013-12-23  12:06:53.776</td><td>205.196.121.163</td><td>178.20.224.151</td><td>6</td><td>80</td><td>64450</td><td>1552</td><td>256</tr>
..................
</table>

Open in new window



Mail code :


$to = 'c.e@xxxxx.com.tr';
$cc = 'abuse@xxxxx.com.tr';
$from = 'no-reply@noc.xxxxx.com.tr';
$subject = 'Attack Information - '.$ipaddress;

 $message = ' <style type="text/css">';
 $message .= ' .auto-style1 {';
 $message .= ' font-size: small;';
 $message .= ' }';
 $message .= ' .auto-style2 ';
 $message .= ' font-size: 8px;';
 $message .= ' color: #666666;';
 $message .= ' }';
 $message .=   '   </style>';
$message .= 'We realize that your ip address attacking to our ip address ("'.$victim.'") Please stop the attacker. <br/><br/>';


 open (MYFILE, $fileurl);
 while (<MYFILE>) {
        chomp;
        $message .= "$_ <br/>";
 }
 close (MYFILE);




 $message .= '<p>';
 $message .= ' <span class="auto-style2"><strong>Noc Team / xxxxx Telekom</strong></span><br class="auto-style1" />';
 $message .=   '        <br class="auto-style1" />';
 $message .= ' <span class="auto-style1">Email:&nbsp;info@xxxxx.com.tr&nbsp;<br/> </span>';
 $message .= ' <span class="auto-style1">Office:&nbsp;+90 - (232) 332 01 05&nbsp;/ Fax:+90 - (232) 332 01 05&nbsp; </span>';
 $message .= ' <br class="auto-style1" />';
 $message .= ' <span class="auto-style1">68 Sk. No:17 /3 Üçkuyular / Izmir </span>';
 $message .=   '        <br class="auto-style1" />';
 $message .= ' <a href="https://www.xxxxx.com.tr"><span class="auto-style1">https://www.xxxxx.com.tr</span></a>';


$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Data     => $message
                 );


$msg->attr("content-type" => "text/html");

Open in new window




cat result of txt file :

[root@sflow ispsend]# cat 1387793258-205.196.121.163.txt
timestampfromfile       srcIP   dstIP   prot    srcPort dstPort octets  packets
2013-12-23 12:06:44.876 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:44.926 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.454 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:45.563 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.645 205.196.121.163 178.20.224.151  6       80      64448   1522    256
2013-12-23 12:06:45.649 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.650 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.736 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:45.739 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:45.800 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.802 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.806 205.196.121.163 178.20.224.151  6       80      64448   1522    256
2013-12-23 12:06:45.866 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.868 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:45.954 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:46.045 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:46.437 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:46.455 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:46.466 205.196.121.163 178.20.224.151  6       80      64448   1522    256
2013-12-23 12:06:46.469 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:46.471 205.196.121.163 178.20.224.151  6       80      64454   446     256
2013-12-23 12:06:46.561 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:46.572 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:46.691 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:46.755 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:46.932 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:46.936 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:46.942 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:47.192 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:47.198 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:47.201 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:47.207 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:47.268 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:47.270 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:47.271 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:47.386 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:47.496 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:47.499 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:47.642 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:47.664 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:47.666 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:47.738 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:47.788 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:47.876 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:47.886 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:47.928 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:48.041 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:48.068 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:48.182 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:48.400 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:48.426 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:49.800 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:49.983 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.520 205.196.121.163 178.20.224.151  6       80      64448   1522    256
2013-12-23 12:06:50.523 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.529 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:50.540 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:50.553 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.559 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:50.573 205.196.121.163 178.20.224.151  6       80      64454   1522    256
2013-12-23 12:06:50.587 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.589 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.598 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:50.600 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:50.604 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:50.617 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:50.623 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:50.631 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:50.644 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.648 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:50.649 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:50.652 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:50.678 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:50.679 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:50.800 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:51.039 205.196.121.163 178.20.224.151  6       80      64454   1522    256
2013-12-23 12:06:51.041 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:51.049 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.070 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:51.072 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:51.081 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.082 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.089 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.119 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.122 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:51.124 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:51.126 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:51.143 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:51.154 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.162 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:51.165 205.196.121.163 178.20.224.151  6       80      64454   1522    256
2013-12-23 12:06:51.192 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:51.211 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:51.268 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:51.675 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:51.691 205.196.121.163 178.20.224.151  6       80      64453   1522    256
2013-12-23 12:06:52.176 205.196.121.163 178.20.224.151  6       80      64449   866     256
2013-12-23 12:06:52.177 205.196.121.163 178.20.224.151  6       80      64448   1522    256
2013-12-23 12:06:52.180 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:52.188 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:52.210 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:52.655 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:52.894 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:52.896 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:53.183 205.196.121.163 178.20.224.151  6       80      64439   1522    256
2013-12-23 12:06:53.299 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:53.403 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:53.417 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:53.737 205.196.121.163 178.20.224.151  6       80      64451   1522    256
2013-12-23 12:06:53.739 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:53.776 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:53.778 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:53.786 205.196.121.163 178.20.224.151  6       80      64450   1522    256
2013-12-23 12:06:53.804 205.196.121.163 178.20.224.151  6       80      64449   1522    256
2013-12-23 12:06:53.806 205.196.121.163 178.20.224.151  6       80      64453   1522    256

Open in new window







sample mail output :


http://prntscr.com/2dfzd6
0
Comment
Question by:3XLcom
  • 2
3 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 39737020
There are multiple ways to build the table and all of them require you to parse each line of your data file into the required fields for each cell in each row.

The approach I'd take would be to use the HTML::Template module.  You would need to create an html email template file.  As you parse each line of the data file putting that data into a hash which is then pushed onto an array.  That array would then be passed to the template to build your email content which is then passed to the MIME::lite object to send the email.

Doing it this way would remove all html specific code from the script which simplifies your script and its maintenance.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 39738304
Here's an example using HTML::Template.

I first created an email.tmpl file which only contains the table structure.  You can expand it to include the css and anything else you want.
<table>
<tr>
  <td>timestampfromfile</td>
  <td>srcIP</td>
  <td>dstIP</td>
  <td>prot</td>
  <td>srcPort </td>
  <td>dstPort</td>
  <td>octets</td>
  <td>packets</td>
</tr>
<tmpl_loop name="email_table">
<tr>
  <td><tmpl_var name=date> <tmpl_var name=time></td>
  <td><tmpl_var name=srcIP></td>
  <td><tmpl_var name=dstIP></td>
  <td><tmpl_var name=prot></td>
  <td><tmpl_var name=srcPort></td>
  <td><tmpl_var name=dstPort></td>
  <td><tmpl_var name=octets></td>
  <td><tmpl_var name=packets></td>
</tr>
</tmpl_loop>
</table>

Open in new window

Next I created a test scrip that parses you data file and builds the table then outputs it.
#!/usr/bin/perl

use warnings;
use strict;
use HTML::Template;

my $file = '1387793258-205.196.121.163.txt';
open my $fh, '<', $file or die "failed to open '$file' $!";

<$fh>; # disgard header line

my @data;
while (my $line = <$fh>) {
    chomp $line;
    my %row;
    my @fields = qw(date time srcIP dstIP prot srcPort dstPort octets packets);
    @row{@fields} = split ' ', $line;
    push @data, \%row;
    
    # limiting to the first 5 records for demonstation
    last if $. == 5;
}
close $fh;

my $template = HTML::Template->new(filename => 'email.tmpl');
$template->param(email_table => \@data);

my $message = $template->output;
print $message;

Open in new window

Here's the output it generates.
c:\test>example.pl
<table>
<tr>
  <td>timestampfromfile</td>
  <td>srcIP</td>
  <td>dstIP</td>
  <td>prot</td>
  <td>srcPort </td>
  <td>dstPort</td>
  <td>octets</td>
  <td>packets</td>
</tr>

<tr>
  <td>2013-12-23 12:06:44.926</td>
  <td>205.196.121.163</td>
  <td>178.20.224.151</td>
  <td>6</td>
  <td>80</td>
  <td>64439</td>
  <td>1522</td>
  <td>256</td>
</tr>

<tr>
  <td>2013-12-23 12:06:45.454</td>
  <td>205.196.121.163</td>
  <td>178.20.224.151</td>
  <td>6</td>
  <td>80</td>
  <td>64451</td>
  <td>1522</td>
  <td>256</td>
</tr>

<tr>
  <td>2013-12-23 12:06:45.563</td>
  <td>205.196.121.163</td>
  <td>178.20.224.151</td>
  <td>6</td>
  <td>80</td>
  <td>64439</td>
  <td>1522</td>
  <td>256</td>
</tr>

<tr>
  <td>2013-12-23 12:06:45.645</td>
  <td>205.196.121.163</td>
  <td>178.20.224.151</td>
  <td>6</td>
  <td>80</td>
  <td>64448</td>
  <td>1522</td>
  <td>256</td>
</tr>

</table>

Open in new window

0
 

Author Closing Comment

by:3XLcom
ID: 39766866
thank you so much
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now