I want to launch tcpdump on linux shell as a background process using a perl script but I have to parse the output after which i need to kill the process. How can I do this ?
Asked
Active
Viewed 1,032 times
0
-
You can use [`ps`](http://linux.die.net/man/1/ps), [`grep`](http://linux.die.net/man/1/grep) and [`cut`](http://linux.die.net/man/1/cut) to get the pid. – Some programmer dude Oct 10 '12 at 05:48
-
$! holds the process id, get this info right after the process is started. – MeaCulpa Oct 10 '12 at 05:57
1 Answers
1
I Unix, the traditional way is to write small and very specialized programs and pipe them together. Why don't you just pipe tcpdump output into your script stdin?
My perl is a little rusty:
$| = 1;
my $pid = open(DUMP, "tcpdump -i eth1 -l -n 2>\&1 |");
while(<DUMP>) {
# do something
if($some_condition) {
`kill $pid`;
last;
}
}

Paulo Scardine
- 73,447
- 11
- 124
- 153