onlineperluser has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I am trying to run a tcpdump and have perl kill the tcpdump once 10 files have been created by the tcpdump. Here is my code, not sure of my logic.

Thanks,
#!/bin/perl system "tcpdump -i bge1 -s0 -w /tmp/file.out -C 1"; sleep 2; while(true){ <pre>@array1 = `ls -l /tmp | grep files`; $result=@array1+1; if ($result > 3){ $x=`ps -ef | awk '/tcpdump/ && !/awk/ {print $ +2}'`; @y=split(' ', $x); $c=$y[1]; system "kill -9 $c"; print "killing tcpdump..."; }else{ print "!!!\n"; exit; } exit; }

Replies are listed 'Best First'.
Re: kill process when files created...
by jethro (Monsignor) on Jul 03, 2008 at 03:16 UTC
    You seem to count all files in /tmp that have the string 'files' in the name and if you find 3 or more you try to kill tcpdump

    I'm guessing here that you wanted to search for 'file' because that's in the name of the logfiles and the 3 should have been a 10. That would work, but other programs might produce a file with 'file' included in the name. For example previous incarnations of your script

    To be sure to count only logfiles of tcpdump, you could generate a (large) random number (random so that old tcpdump-logs don't count when you restart the program), put that into the name and search for that.

    Also the sleep should be inside the while loop, otherwise your program will use 100% CPU.

    #!/bin/perl -w srand(time()^$$); my $number= int(rand(100000000)); system "tcpdump -i bge1 -s0 -w /tmp/file$number.out -C 1 &"; while(true){ sleep 2; @array1 = `ls -1 /tmp | grep $number`; $result=@array1; if ($result > 9){
    I also changed ls-l to ls -1 which lists one file per line too, but without the chance that the length of a file is equal to the random number.

    I only scimmed over the rest of your code but couldn't find any obvious mistakes there.

    EDIT: pc88mxer found another bug, so I corrected that (the missing '&') in the code.

Re: kill process when files created...
by pc88mxer (Vicar) on Jul 03, 2008 at 03:40 UTC
    There's a real problem with your approach because tcpdump won't be running in the background.

    If you just want to capture 10 packets, why not just tell tcpdump to stop after 10 packets:

    tcpdump -i bge1 -c 10 > /tmp/file.out
    Otherwise have a look at Net::Packet::Dump.
      He wants 10 files with size 1M, not 10 packages. The parameter -C has a unit of 1,000,000 bytes.

      tcpdump seems to have no principal problem with running in the background, just checked it on the command line. It may need some io redirection if called from a script. But it should have at least a '&' at the end to go into the background.

        Good point about the -C option.

        The way the OP is invoking tcpdump it will not get run in the background. Thus the while will not get executed until tcpdump finishes.