LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Security (http://www.linuxquestions.org/questions/linux-security-4/)
-   -   bizarre piping behaviour (http://www.linuxquestions.org/questions/linux-security-4/bizarre-piping-behaviour-348639/)

tom_from_van 07-31-2005 03:57 PM

bizarre piping behaviour
 
Forgive me if this question seems off-topic but I can't really find a forum that seems more apropos for it, and since the purpose is security related, I put it here.
When I grep for non-icmp output from tcpdump, the following works as intended.

tcpdump -n -vvv | grep 'arp' | perl ps2.pl

where 'perl ps2.pl' is the sub-command that runs a script of mine (ps2.pl) under the perl interpretor, using the grepped output of tcpdump. The script is trivial at this point, all it wants to do is take the input being piped to it using the ol' angle brackets thangy:

while(<>){
print "######" . $_;
`play question.wav`;
}

However, when I:

tcpdump -n -vvv | grep 'icmp' | perl ps2.pl

somehow, some way, the output (lines from tcpdump having 'icmp') don't get piped into the script, instead, these lines get printed on the screen, but don't go into the script --- what is happening? I tried some "2>&1" type stuff, to no avail. And like I said, the input gets piped successfully if the search term is 'arp' or other non-icmp terms, but as soon as the term being grepped for is icmp, boom, the output stops going where I want it to.
Help!
:)

Matir 07-31-2005 07:25 PM

I gotta be honest that that's REALLY odd. :) I've never seen such behavior myself. What happens if you remove the '| perl ps2.pl' from the end of it?

tom_from_van 08-01-2005 10:58 AM

Tell me about it! When I go:

tcpdump -n -vvv | grep 'icmp'

and then get GRC to scan me, it prints out output lines, like:

00:53:31.701798 IP (tos 0x0, ttl 119, id 32768, offset 0, flags [none], proto 1, length: 28) 4.79.142.206 > 26.6.8.14: icmp 8: echo request seq 0
00:53:32.187198 IP (tos 0x0, ttl 119, id 32768, offset 0, flags [none], proto 1, length: 28) 4.79.142.206 > 26.6.8.14: icmp 8: echo request seq 0
00:53:32.687328 IP (tos 0x0, ttl 119, id 32768, offset 0, flags [none], proto 1, length: 28) 4.79.142.206 > 26.6.8.14: icmp 8: echo request seq 0

but when I try to pipe these lines into my perl script, they don't get there, although as long as the grep-string isn't 'icmp', the outputted lines WILL get into the script! I know, this is really strange, and I don't have a clue what is happening..
:)

Matir 08-01-2005 01:16 PM

Ok, give the following a try:
Code:

tcpdump -n -vvv | grep icmp 2>&1 | perl ps2.pl
Also, it might help to check that you have the latest version of the GNU coreutils.

tom_from_van 08-01-2005 03:26 PM

No, that's what I thought, too --- at first: It's using 2 STDIN and STDERR or STDOUT, when it should be using just one of them (like I mentioned in the OP, "...tried 2>&1 stuff ...") but it doesn't help.
This is too wierd. When I use:
tcpdump -n -vvv | grep 'icmp' | perl ps2.pl
the output goes to the screen, not the script, but when I use:
tcpdump -n -vvv | grep 'arp' | perl ps2.pl
the output successfully gets directed into the script.

tom_from_van 08-01-2005 03:29 PM

Oh yeah, I forgot to mention I'm running a fresh install of FC4, with "yum -y update" ran every day. Does this mean I have the latest core utilities?

Matir 08-01-2005 03:36 PM

Yeah, you probably do. Just as an FYI, I am unable to reproduce this behavior on any of my boxes.

Edit: FYI, versions are as follows:
tcpdump version 3.8.3
libpcap version 0.8.3
grep (GNU grep) 2.5.1

tom_from_van 08-01-2005 05:29 PM

What I have:

tcpdump version 3.8
libpcap version 0.8.3
grep (GNU grep) 2.5.1

which is odd, given my frequent runs of "yum -y update" --- I would have thought that I'd have the latest versions of everything including tcpdump of which I have version 3.8 and not version 3.8.3 that you have.

tom_from_van 08-01-2005 06:09 PM

Oh well, thank god for the fact there's more than one way to do things --- how does that turbo-geek saying go? tim-toady or smt equally special. :)
I thought I would build as much functionality as I could by cobbling apps together on the command line just as an exercise in command line skills, of which i have a pitifull lack, having been such a recent microsoft defector (I've been running FC4 for a week now). But in the end, I put more functionality in the script instead of the command line with the following:

#!/usr/bin/perl
open ZZ, "tcpdump -n -vvv 2>&1 | " or die "I died because I couldn't start the input pipe $!\n";
print "I seem to have started ... \n";
while(<ZZ>){
if($_=~/icmp/){
print "#---> " . $_;
}
}
close ZZ;
exit 0;

And it works.
I've tried EVERYTHING involving line buffering specifications, wide/narrow character settings, snaplen, the works. Still, as soon as the grepped-for term is icmp and not ANYTHING else, output stops going into the script, so I gave up and decided to use regex's within the script instead of grepping on the command line. It's probably better this way anyway. But I'm still absolutely mystified as to why changing the grep-string to "icmp" has the effect that it does.


All times are GMT -5. The time now is 04:25 PM.