Tcpdump on many machines from single script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tcpdump on many machines from single script
# 1  
Old 09-26-2018
Tcpdump on many machines from single script

Hi all, new to the forum and rusty with my scripting. I am trying to put together a quick and dirty script that will kickoff a tcpdump on multiple machines. Then, another script that will reach out (at a later time) to stop the processes and retrieve the data. It seems fairly easy conceptually and will most likely consist of repeating lines with different username@<ip addresses> so I am trying to get just one to work for now.
Digging around the interweb has simply confused me and I could really use some help.

The usernames for the machines all have passwords (sorry, using keys is not an option) so I was looking at using expect like:

Code:
#!/usr/bin/expect -f
spawn ssh <username>@<ip_address> "/usr/sbin/tcpdump -i any -w filename.dump &"
expect "assword:"
send "<password>\r"

... then repeat for another server, leaving tcpdump running on each box when I'm done

Obviously, this does not work or I wouldn't be here. In this case, the script runs but when I look on the server, tcpdump is not running. I had tried using "interact" after the password and removing the & but that just left my script hanging and not moving on to kickoff the next tcpdump.

Any suggestions/corrections would be appreciated
# 2  
Old 09-27-2018
Quote:
Originally Posted by k9sar
In this case, the script runs but when I look on the server, tcpdump is not running.
The problem is not your script but the way UNIX is working: when a process starts it is assigned a terminal - usually the one it was started at. Once this terminal goes away the process ist terminated too. Now, you log on to a system by using ssh. That you do it from a script doesn't matter at all. Inside this session you start a program - tcpdump - and then kill the session. This way, the terminal ceases to exist and therefore the program is terminated too.

You probably thought that you prevented that by sending the process to background, but this is not the case. What you need (in addition to sending it to background) is the nohup keyword. "nohup" is short for "no termination on hangup" and it prevents exactly that behavior: the process will not be terminated once the terminal goes away (the session "hangs up" - this is from the time when sessions were serial lines and mostly dial-up).

If you do it with "interact" your script doesn't hang at all: it is executing the "ssh"-command but since this is never terminated it will never get to the next command. Therefore it seems to hang.

Still i'd like to suggest that you forego the whole business with expect. Instead of writing passwords in clear-text into your script (it doesn't matter if they come from a file or your scripts text directly - clear-text is clear-text and whatever encryption you will use the script will have to decrypt it automatically) you should exchange ssh-keys for the system-/user-combinations you want to process and use these. Then you can just build a textfile with the systems and users and your script will be a simple loop, like this:

Code:
# cat /input/file
user1@systemA
user2@systemB
user3@systemC

Code:
#! /bin/ksh

while read LINE ; do
     ssh "$LINE" "command"
done < /input/file

exit 0

One last thing: if you use nohup you should expressly redirect all possible output of the process, e.g.

Code:
nohup /some/command >normal.log 2>/dev/null &

because otherwise any output would be sent by mail (!) to either the user or root internally. You don't want that.

I hope this helps.

bakunin

Last edited by bakunin; 09-27-2018 at 05:17 AM..
This User Gave Thanks to bakunin For This Post:
# 3  
Old 09-27-2018
One correction, by default ssh reads from stdin and competes with the read command.
Quick fix: ssh -n...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

TCPdump

I've recently started learning to use TCPdump, and I find it pretty interesting. There's one thing I don't understand. When I tell it to capture packets on, say, the WiFi interface en1, it often captures packets sent or received by other hosts on the network. How can it do this? My... (3 Replies)
Discussion started by: Ultrix
3 Replies

2. Shell Programming and Scripting

Script to change the file at one go on different UNIX machines

Hi Folks , I have a query that is I have a server (unix machhine) to which I login thru winscp or putty , mostly I use putty and at a particular location there is one xml(abc.xml) while which I change , let say 1) I login to first unix box hostname :- ccc74 username ... (1 Reply)
Discussion started by: tuntun27272727
1 Replies

3. Shell Programming and Scripting

Shell Script to find out if a process is running on any all machines

Hi All, I have been a guest visitor from a long time and this forum is wonderful. I finally am a member of this forum too, so i am here stuck with a shell script that i was trying to write My requirement is that i should be able to create a shell script which will check if a process is running on... (3 Replies)
Discussion started by: Rex0226
3 Replies

4. Shell Programming and Scripting

tcpdump script to parse "packers captured" details

I want a script that would do as:- a) gives me packet capture account for each time it runs. b) be able to run at a particular time for specific period time duration (1 min). c) for each time it runs it saves the time / day. Is there a way where i can capture the details as seen in the... (2 Replies)
Discussion started by: lazerz
2 Replies

5. UNIX for Dummies Questions & Answers

run script accross machines using ssh

Hi all I have to run certain set of commands on two machines, the two machines see the same home, it's mounted from the same place. The problem is that i have to ssh to a certain machine "which is slow unfortunately" that has the license to run a tool and i want to return to the original machine... (3 Replies)
Discussion started by: amr elhosiny
3 Replies

6. Shell Programming and Scripting

TCPdump script

I'm new to the Unix/Linux world. I have taken classes and played with a few simple scripts but never had a real world application. Here is my problem. What I need to do is every 15min between 8am and 5pm, run tcpdump -s 2000 -w flowroute-0000.pcap where the "0000" is the current time. ... (4 Replies)
Discussion started by: Nasasdge
4 Replies

7. Shell Programming and Scripting

write a script to parse some tcpdump output

i am trying to write a script to parse some tcpdump output, in each line of the tcpdump output, I know for sure there are 3 keywords exist: User{different usernamehere} NAS_ipaddr{different ip here} Calling_station{ip or dns name here} But the positions for these 3 keywords in the... (4 Replies)
Discussion started by: fedora
4 Replies

8. Shell Programming and Scripting

Help with script, trying to get tcpdump and rotate the file every 300 seconds

Greetings, I just started using scripting languages, im trying to get a tcpdump in a file, change the file name every 5mins ... this is what i have but its not working ... any suggestions? #!/bin/bash # timeout.sh #timestamp format TIMESTAMP=`date -u "+%Y%m%dT%H%M%S"` #tdump =`tcpdump... (3 Replies)
Discussion started by: livewire
3 Replies

9. UNIX for Advanced & Expert Users

Running a script on multiple machines

To clear the web cache on my web server, I run this command: find $APACHE_HOME/cache/plsql/plsql -type d -name "*" -exec rm -R {} \; To clear the cache on all the web servers(we have 4), I log on to any one machine, clear its cache, ssh to another machine, clear cache etc; Is there any way... (8 Replies)
Discussion started by: nattynatty
8 Replies

10. Programming

How To Use tcpdump

I have two net-card. one is 172.16.24.99(ENG) ,another is 172.16.25.99(ENG-B). Both masks is 255.255.255.0. I will monitor data on the tcp port 8055 in ENG, How do I set option of tcpdump command (2 Replies)
Discussion started by: chenhao_no1
2 Replies
Login or Register to Ask a Question