Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: capture output of tcpdump and PID

by thargas (Deacon)
on May 22, 2012 at 18:48 UTC ( #971848=note: print w/replies, xml ) Need Help??

in reply to capture output of tcpdump and PID

Answering the first question first, you do indeed have two processes; that's what the | does in the open. See doc:perlopentut.

I'm not sure what want with the second one. If you mean that you want to stop after a specified amount of time, then you could just use alarm() to set a timer for the desired amount of time and exit the loop at that point. Like (untested):

my $done = 0; local $SIG{ALRM} = sub { $done = 1; }; alarm 10; # or whatever while (not $done and $output = <STDIN>) { print $output; # don't need newline as it's there already }

Replies are listed 'Best First'.
Re^2: capture output of tcpdump and PID
by mike928 (Initiate) on May 24, 2012 at 14:38 UTC

    Thanks, i also tried another way to stop the process, and it's quite simple... i just close(LABEL)

    I also tried the alarm code, and it works

    The alarm code isn't somehow similar to this:

    open (CAPTURE,"sudo /usr/sbin/tcpdump -i wlan0 |"); $startTime = time(); while ( $line = <CAPTURE> ) { ### some code $endTime = time(); $t = $endTime - $startTime; if($t>10) { close(CAPTURE); last; } }

    how could i pause at regular intervals the tcpdump process to switch between wifi channels 1, 6, 11 without stopping (closing) the pipe ?

    Any idea would be appreciated

      How do you set/change the channel? If you do it by giving tcpdump an argument, then I suspect you're out of luck. You'll just have to stop the process and restart it with the new channel setting. If the channel can be changed with some other utility while tcpdump is running, then it should be fairly simple: read from the pipe until you want to change it (maybe based on how much time has elapsed since you started), and then run the other utility, then continue reading from the pipe. There may be some buffered output from tcpdump already waiting at the moment you change the channel. If that's a problem, you may have to do something more fancy.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

      The problem with just adding the time-comparison to the loop is that if nothing comes from tcpdump, the loop hangs waiting for something and your timeout code is never invokes. At least not until after something does come in. Perhaps this is not an issue in your case.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971848]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-06-22 06:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.