0

I am writing a bash script that reads a file and for each item in column 1, runs a command on 2-column file. It needs to ssh into each hostname (column 1) and kill a PID (PID # in column 2). Here's the code I'm trying, which isn't working (NOTE: this example does not run the kill command but asks script to ps the PID):

while read host pid; do
        /usr/bin/sshpass -f ~/.pw /usr/bin/ssh -q  -o "StrictHostKeyChecking=no" -o ConnectTimeout=5 $host ps -ef| egrep -e $pid
done < $UNIQ_FILE

The $UNIQ_FILE is formatted like this:

192.168.22.199  55829
192.168.22.142  662
...

The script is running the command on the first line of the file, and then exits.

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
Nancy
  • 1

1 Answers1

0

You need to use -n option in your ssh command

-n Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background. A common trick is to use this to run X11 programs on a remote machine. For example, ssh -n shadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be automatically forwarded over an encrypted channel. The ssh program will be put in the background. (This does not work if ssh needs to ask for a password or passphrase; see also the -f option.)

while read host pid; do /usr/bin/sshpass -f ~/.pw /usr/bin/ssh -n -q -o "StrictHostKeyChecking=no" -o ConnectTimeout=5 $host ps -ef| egrep -e $pid done < $UNIQ_FILE
Rakesh Gupta
  • 3,507
  • 3
  • 18
  • 24