#!/usr/bin/perl use strict; my( $header, $packet ) = ('', ''); open STDIN, "/usr/sbin/tcpdump -lx -s 1024 dst port 80 |"; #die "Could not open tcpdump" unless $?; while( <> ) { chomp; if( /^\S+/ ) { print_packet( $header, $packet ); $header = $_; $packet = undef; next; } $packet .= $_; } sub print_packet { my( $header, $packet ) = @_; $packet =~ s/\s//g; $packet =~ s/.{104}//; # remove protocol info return unless length $packet > 0; $packet =~ s/0d0a0d0a.*/0d0/i; # Just keep the HTTP header my $pretty = ''; BYTE: while( $packet =~ m/\G([0-9a-f]{2})\s*/ig ) { my $hex = hex $1; if( $hex == 0x0D ) { $pretty .= "\n"; next BYTE } if( $hex == 0x0A ) { next BYTE } $pretty .= do { if( $hex < 0x20 or $hex > 0x7f ) { '.' } else { chr $hex } }; } if( $pretty =~ m/^(?:HTTP|GET|POST)/ ) { print_header( $header ); print $pretty; } return length $packet; } sub print_header { my $header = shift; my ( $time, $src, undef, $dst, ) = split /\s+/, $header; $time =~ s/\..*//; $dst =~ s/:$//; print "-" x 73, "\n"; print "Time: $time Src: $src Dst: $dst\n"; }