Friday, July 25, 2008

Hexa to Decimal Conversion in Perl

k1 contains the hexadecimal value . to convert it into decimal we need
to do the following 2 lines coding.

$k1='0015';
$k1=hex($k1);
$dec = sprintf("%d", $k1);

Thursday, July 24, 2008

Three Level Hash in Perl

Here i have tried to put code for the network packet information

in the following pattern.



%ip_port=();

$ip_port{10.1.1.1}{'TCP'}{25}=100;
$ip_port{10.2.1.1}{'TCP'}{23}=10;
$ip_port{10.1.2.1}{'UDP'}{53}=1100;
$ip_port{10.111.1}{'TCP'}{80}=1030;
$ip_port{10.1.1.1}{'UDP'}{53}=1200;
$ip_port{10.1.12.1}{'UDP'}{53}=1050;
$ip_port{10.13.1.1}{'TCP'}{123}=11100;
$ip_port{10.11.1.1}{'TCP'}{443}=10340;


print "<table><tr><td>SouceIP </td><td>Porotocol</td> <td>Port </td><td>Total Packet</td></tr>";

foreach $key(keys %ip_port)
{
       print $key." => ".$ip_port{$key}."\n";

        print "\n".$key." => \n";
                foreach $k1(keys %{$ip_port{$key}})
                {
                       print $k1." => ";

                        foreach $k2(keys  %{$ip_port{$key}{$k1}})
                        {

                               print $k2." => ";

                                $value=$ip_port1{$key}{$k1}{$k2};
                        print "<tr><td>$key</td><td>$k1</td><td>$k2</td><td>$value</td></tr>";
                        }

                }
}
print "</table>";


Example Output

Source IP Protocol Port Total Paclets
x.x.x.x
TCP 491 2
x.x.x.x TCP 25 13
x.x.x.x TCP 25 7
x.x.x.x ICMP 0 3
x.x.x.x ICMP 771 1
x.x.x.x
TCP 25 12
x.x.x.x
ICMP 0 2