Monday, July 25, 2011

Reading pcap files with Python

#!/usr/bin/python

import sys
import pcapy
from impacket import ImpactDecoder, ImpactPacket
import re

def main(argv):

        try:
                cap = pcapy.open_offline(argv[1])

                (header, payload) = cap.next()

                while header:
                        (seconds, micros) = header.getts()

                        # Parse the Ethernet packet
                        decoder = ImpactDecoder.EthDecoder()
                        ether = decoder.decode(payload)

                        # Parse the IP packet inside the Ethernet packet
                        iphdr = ether.child()

                        # Parse the TCP packet inside the IP packet
                        tcphdr = iphdr.child()

                        # Get the source and destination IP addresses
                        src_ip = iphdr.get_ip_src()
                        dst_ip = iphdr.get_ip_dst()

                        if tcphdr.child() <> None:
                                body = tcphdr.child().get_packet()

                                isFIX = re.match('8=FIX', body)
                                if isFIX <> None:
                                        print "%d.%06d %s" % (seconds, micros, body)

                        (header, payload) = cap.next()

        except pcapy.PcapError:
                pass

if __name__ == "__main__":
        main(sys.argv)

Wednesday, July 20, 2011

Date formatting in Perl

use Date::Format;
my $Time = time2str("%D", time());

Give something like 07/20/11

Package libtimedate-perl (in hardy, Ubuntu 8.04.3 LTS)

dpkg -S Date::Format

Sending a text file in the body of an email using Perl

In Perl there is a package called Mail::Sendmail that could be used to send a simple text message.

The package for the module is libtimedate-perl (in hardy, Ubuntu 8.04.3 LTS)

dpkg -S Mail::Sendmail

#!/usr/bin/perl

use strict;
use warnings;

use Mail::Sendmail;

# Read the file into a string to put it into the mail hash variable
open FH, 'file.txt' or
        die "Error reading file.txt file: $!\n";
my @Lines = ;
chomp @Lines;
close FH;
my $Lines = join "\n", @Lines;

my $Recipients = 'somebody@mail.example';

my $Subject = 'Testing mail in Perl';

my %mail = (
        To => $Recipients,
        From => 'testing@mail.example',
        Subject => $Subject,
        Message => $Lines
);

$mail{Smtp} = 'mail.example';

if (sendmail %mail) {
        print "Mail sent OK.\n"
} else {
        print "Error sending mail: $Mail::Sendmail::error \n"
}

print "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;

__END__

Wednesday, July 13, 2011

Yum groups and repositories

Yum supports the group commands
  • grouplist
  • groupinfo
  • groupinstall
  • groupremove
  • groupupdate
Groups are read from the "group" xml metadata that is optionally available from each repository. If yum has no repositories which support groups then none of the group operations will work.
  • grouplist
    yum grouplist [hidden]
    
This will list the installed and available groups for your system in two separate lists. If you pass the optional 'hidden' argument then all of the groups which are set to 'no' in the group xml tag.
  • groupinfo
    yum groupinfo groupname
    
This will give you detailed information for each group including: description, mandatory, default and optional packages.
  • groupinstall, groupupdate
    yum groupinstall groupname
         yum groupupdate groupname
    
Despite their differing names both of these commands perform the same function. They will attempt to install/update all of the packages in the group that are of the types 'default' or 'mandatory' (by default). (To change this types of packages edit the value of the group_package_types option in yum.conf.) And they will install any additional dependencies needed by any of the installing/updating packages.
  • groupremove
    yum groupremove groupname
    
This will remove all packages, of any type, in the named group. It will also remove any package that depends on any of these packages.

Thursday, June 30, 2011

Using grep to extract specific FIX protocol tags values from a file

grep -o -w '\(48\|55\)=[^[:cntrl:]]\+' FILE | perl -pe 's/(\d+=[^\n]+)\n/\1~/' | perl -pe 's/([^~]+~[^~]+)~/\1\n/g'

grep -o -w '\(48\|55\)=[^[:cntrl:]]\+' FILE | tr '\n' '~' | perl -pe 's/([^~]+~[^~]+)~/\1\n/g'

grep -o -w '\(48\|55\)=[^[:cntrl:]]\+' FILE | sed 'N;s/\n/ /'

-o Print only the matched parts of a matching line
-w Select lines containing matches that form whole words (a whole char is a letter, digit and underscore)

Tuesday, June 28, 2011

Static routes after restart/reboot in Fedora/RedHat/CentOS

Static routing is the term used to refer to the manual method used to set up routing. An administrator enters routes into the router using configuration commands. This method has the advantage of being predictable, and simple to set up. It is easy to manage in small networks but does not scale well.

Question: How can I save static routes I set up in my Fedora/RedHat/CentOS Linux after I reboot server?

Answer: In Fedora Linux (or RedHat, CentOS) you can set up static routes for certain network interface (for example eth1) by editing file /etc/sysconfig/network-scripts/route-eth1.

For example, you have to save static route added by the following command:

route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.100.1 dev eth1

To do it, just add the following line to /etc/sysconfig/network-scripts/route-eth1:

ADDRESS0=192.168.0.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.100.1

The alternate way of doing it is to put the routes in /etc/sysconfig/static-routes. So the above route would like this like in the static-routes file:

any net 192.168.0.0 netmask 255.255.255.0 gw 192.168.100.1

Where any is specified above you can also set specific interfaces (such as eth1).

Here is the part /etc/init.d/interfaces code:

# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi

Looks like any must be at the beginning of each line in /etc/sysconfig/static-routes.

/usr/share/doc/initscripts-${VERSION}/sysconfig.txt is a good place to look for these things as well