I want to capture packets using tcpdump over a certain period of time. However, if there are not packets captured in that time period I would like the program to exit. tcpdump does not allow you to specify a time period just packets based on time period. So if I receive 0 packets over a specific time period it should exit. The source code (a mod of an old sniffer program) is as follows:
open (fileOUT, ">> /results.out") or dienice ("Can't open file for wr +iting"); close(fileOUT); open (STDIN,"/usr/sbin/tcpdump -lnx -s 1024 dst port 80 |"); while (<>) { if (/^\S/) { last unless $LIMIT--; while ($packet=~/(GET|POST|WWW-Authenticate|Authorization).+/g) + { open (fileOUT, ">> /results.out") or dienice ("Can't open fi +le for writing"); flock(fileOUT, 2); seek(fileOUT, 0, 2); print fileOUT "$client -> $host\t$&\n"; close(fileOUT); } undef $client; undef $host; undef $packet; ($client,$host) = /(\d+\.\d+\.\d+\.\d+).+ > (\d+\.\d+\.\d+\.\d+) +/ if /P \d+:\d+\((\d+)\)/ && $1 > 0; } next unless $client && $host; s/\s+//; s/([0-9a-f]{2})\s?/chr(hex($1))/eg; tr/\x1F-\x7E\r\n//cd; $packet .= $_; }
I've tried using gmtime but it still continues reading even when it reaches a set time. Should I make the tcpdump a thread and then kill that thread after a specific time? Any comments/suggestions would be great. Thanks.

2004-11-22 Edited by Arunbear: Changed title from 'tcpdump', as per Monastery guidelines


In reply to tcpdump: setting a conditional timeout by intranetman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.