Hi brian,
I modified your script to match my local config ("eth0" instead of "en1", removed "or src port 80" since I don't need that, and ran it as root directly, so no need for sudoing before). However, I got no output when using it. Here's what I have:
#!/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";
}
Cheers.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|