Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


A very simple script does not work

A very simple script does not work

A very simple script does not work

Hi Experts,

I have a very simple script listed below:


#! /usr/bin/perl

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


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

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:


% ./test.pl
0 : :200 OK
1 : :200 OK
1 : :200 OK

1, $cmd = ssh root@ "/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@ "cd /tmp; ./go_load 2>/dev/null" 2>/dev/null 

After the run, the file '/tmp/mytcpdump.pcap' on 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:


Step 1)
% ssh root@ "ls -l /tmp/mytcpdump.pcap"
ls: cannot access /tmp/mytcpdump.pcap: No such file or directory

Step 2)
% ssh root@ "/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@ "ls -l /tmp/mytcpdump.pcap"
-rw-r--r-- 1 root root 0 Apr 14 04:03 /tmp/mytcpdump.pcap

Step 4)
% ssh root@ "cd /tmp; ./go_load 2>/dev/null" 2>/dev/null
0 : :200 OK
1 : :200 OK
1 : :200 OK

Step 5)
% ssh root@ "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.

RE: A very simple script does not work


Not sure, but my first thought is that you hit the good old problem with running ssh from a script, which usually is solved by passing it the -n option :

Quote (man ssh)

     -n      Redirects stdin from /dev/null (actually, prevents reading from
             stdin).  This must be used when ssh is run in the background 
Give it a try.


RE: A very simple script does not work

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.

RE: A very simple script does not work


ssh root@ "/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

RE: A very simple script does not work

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close