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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ssh "<cmd>" in a loop

Status
Not open for further replies.
Apr 22, 2002
48
US
I am trying to do this:

Code:
#!/bin/ksh
while read HOST 
do
  echo "Checking $HOST"
  ssh $HOST "ls"
done <listofhosts

The file "listofhosts" has one host name per line (about 50 lines). ssh keys have been correctly copied to all the hosts. The script works fine for the first host in the file but comes out (normally, without any error) after that, without looping thru to the next host. If I simply comment out the "ssh" line, the script behaves normally. I am not sure what is the restriction with ssh in such a usage. Using scp instead of ssh allows the script to loop normally. If more than one ssh command is put in the loop, all of them execute exactly once, so script is not even breaking after the first ssh.

Does any one know why ssh above would cause the script not to loop? And, is there a workaround? Essentially, I need to run a command on each of the hosts. I do not have the luxury of using telnet, expect or perl in this environment.

Thanks
 
Never mind. I found the answer in one of the other posts in this forum. Solution is to use "ssh -n".

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top