Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A very simple script does not work 1

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
0
0
US
Hi Experts,

I have a very simple script listed below:
Code:
#! /usr/bin/perl

my $cmd = "ssh root\@198.18.154.133 \"/a/sbin/tcpdump -v -n -i eth0 port 11640 -w /tmp/mytcpdump.pcap 2>/dev/null\" 2>/dev/null &";

system($cmd);

my $cmd2 = "/usr/bin/ssh root\@198.18.154.130 \"cd /tmp; ./go_load 2>/dev/null\" 2>/dev/null";
system($cmd2);

print "\n";
print "1, \$cmd = $cmd\n";
print "2, \$cmd2 = $cmd2\n";

Where ./go_load is binary which runs curl a few times in parallel/serial.

A sample run would look like this:
Code:
% ./test.pl
0 :[URL unfurl="true"]http://198.18.154.171/[/URL] :200 OK
1 :[URL unfurl="true"]http://198.18.154.171/[/URL] :200 OK
1 :[URL unfurl="true"]http://198.18.154.171/[/URL] :200 OK

1, $cmd = ssh root@198.18.154.133 "/a/sbin/tcpdump -v -n -i eth0 port 11640 -w /tmp/mytcpdump.pcap 2>/dev/null" 2>/dev/null &
2, $cmd2 = /usr/bin/ssh root@198.18.154.130 "cd /tmp; ./go_load 2>/dev/null" 2>/dev/null

After the run, the file '/tmp/mytcpdump.pcap' on 198.18.154.133 is empty, which is not expected.

However, if I ran the two cmds manually at the prompt, everything is just fine. A sample manual run is below:
Code:
Step 1)
% ssh root@198.18.154.133 "ls -l /tmp/mytcpdump.pcap"
ls: cannot access /tmp/mytcpdump.pcap: No such file or directory

Step 2)
% ssh root@198.18.154.133 "/a/sbin/tcpdump -v -n -i eth0 port 11640 -w /tmp/mytcpdump.pcap 2>/dev/null" 2>/dev/null &
[2] 26257

Step 3)
% ssh root@198.18.154.133 "ls -l /tmp/mytcpdump.pcap"
-rw-r--r-- 1 root root 0 Apr 14 04:03 /tmp/mytcpdump.pcap

Step 4)
% ssh root@198.18.154.130 "cd /tmp; ./go_load 2>/dev/null" 2>/dev/null
0 :[URL unfurl="true"]http://198.18.154.171/[/URL] :200 OK
1 :[URL unfurl="true"]http://198.18.154.171/[/URL] :200 OK
1 :[URL unfurl="true"]http://198.18.154.171/[/URL] :200 OK

Step 5)
% ssh root@198.18.154.133 "ls -l /tmp/mytcpdump.pcap"
-rw-r--r-- 1 root root 4096 Apr 14 04:04 /tmp/mytcpdump.pcap

So the final step shows the /tmp/mytcpdump.pcap contains some thing, which is expected and proves all commands are working as designed.

I hope I have made myself clear. Thanks for help.
 
Hi

Not sure, but my first thought is that you hit the good old problem with running [tt]ssh[/tt] from a script, which usually is solved by passing it the [tt]-n[/tt] option :
man ssh said:
[pre]
-n Redirects stdin from /dev/null (actually, prevents reading from
stdin). This must be used when ssh is run in the background[/pre]
Give it a try.

Feherke.
feherke.ga
 
Thank you, Feherke, for your help.

I found the solution. All I did was to let it sleep for one second after tcpdump. The '-n' switch does not make the difference, though. But, again, thank you. It's still good to know the '-n' switch.
 
ssh root@198.18.154.133 "/a/sbin/tcpdump -v -n -i eth0 port 11640 -w /tmp/mytcpdump.pcap 2>/dev/null" 2>/dev/null &

I'm not familiar with tcpdump, but a couple of points...

The 2>/dev/null will (I think) redirect any error messages to /dev/null. So if whatever is going wrong is displaying an error message that would help you figure out what's happening, you won't see it.

The & at the end causes the command to be executed in the background, so it may not have finished running by the time you check the output file. Given that sleeping for a second appears to fix the issue, this looks like it may be what's happening. If you need these things to happen in order, consider removing the & altogether.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Hi Chris,

Thanks for your input. Tcpdump can send lots of messages through stderr, even if it's not an error at all. That's why I am using 2>/dev/null. In addition, tcpdump cannot exit out by itself. In a script, you can either run it in a child process or in background.

Thanks agian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top