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...if
my logic is screwy
Thanks,
system "tcpdump -i bge1 -s0 -w /tmp/file.out -C 1";
sleep 2;
while(true){
@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;
Why bother with perl? With the tcpdump command that you have, you
get about 100 bytes/packet. So if you want 10 x 1 MB, then you want
100,000 packets. -c 100000
** Posted from http://www.teranews.com **
> 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...if
> my logic is screwy
Your logic isn't screwy, it's misunderstanding.
> system "tcpdump -i bge1 -s0 -w /tmp/file.out -C 1";
I'm not the B<tcpdump> expert, but B<if> my understanding of tcpdump(8)
is right, then you'll never get out of B<system>. If you use I<-c> (as
smallpond suggested) then B<tcpdump> B<will> exit (apparently you don't
need B<kill> in that case).
OK, if you really want to write shell scripts in Perl do it in Perl.
> sleep 2;
I believe, you missed B<sleep> inside loop.
> while(true){
Show your B<real> code! What the fsck is that "true"?
> @array1 = `ls -l /tmp | grep files`;
> $result=@array1+1;
$result = (() = </tmp/file.out.*>) + 1;
(He-he, I was beaten hardly a week before for missing that.)
> if ($result > 3){
if((() = </tmp/file.out.*>) > 2)
and you don't need to increment.
> $x=`ps -ef | awk '/tcpdump/ && !/awk/ {print
> $2}'`;
$x = (map { m{(\d+)}; }
map { readlink; }
</proc/[0-9]*/exe>)[0];
Since you seem to be root, you'll have permissions to read those
symlinks.
> @y=split(' ', $x);
> $c=$y[1];
$c = (split m{\s+}, $x)[1];
> system "kill -9 $c";
kill 9, $c;
waitpid $c, 0;
> print "killing tcpdump...";
Use B<Proc::Background> and you'll automagically would know the PID of
B<tcpdump>. If you don't want to bother with B<Proc::Background>, then
C<perldoc perlfork> is good reading.
> }else{
> print "!!!\n";
> exit;
Either that must be B<last> or you don't need the second B<exit>.
Anyway you don't need the second B<exit> since you've better just fall
out of script.
> }
> exit;
And as ever: C<use strict> and C<use warnings> are your best friends.
Lexical filehandles and 3-arg B<open> are your good friends.
--
Torvalds' goal for Linux is very simple: World Domination
split ' ' and split m{\s+} do different things so the list element ()[1]
may not return the expected result depending on whether there is leading
whitespace in $x.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
> split ' ' and split m{\s+} do different things so the list element ()[1]
> may not return the expected result depending on whether there is leading
> whitespace in $x.
I agree, but leading space in B<ps> output would be a big surprise.