LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   script running ssh commands (via loop) exiting after first iteration (https://www.linuxquestions.org/questions/linux-server-73/script-running-ssh-commands-via-loop-exiting-after-first-iteration-840120/)

kirecali 10-24-2010 07:51 AM

script running ssh commands (via loop) exiting after first iteration
 
Hello everyone,

One of my servers needs to issue an "apt-get update" command on each of the clients in a lab. However, after the first update, it exits. In trying to replicate the problem with a simpler script, I created the following which fetches the listings of multiple directories from a client:

NUM=0
cat paths | while read DIR; do
ssh server-name ls $DIR > /tmp/$NUM.txt
NUM=`expr $NUM + 1`
done

Again, after the first iteration, the script exists. I tried the following line too, but it made no difference:

bash -c ssh server-name ls $DIR > /tmp/$NUM.txt

The only thing that worked so far is this

xterm -e ssh server-name ls $DIR > /tmp/$NUM.txt

Of course, this is far from ideal since a GUI gets involved.

Any thoughts?

Thanks much.

prayag_pjs 10-25-2010 01:42 AM

To achieve this :

Quote:

One of my servers needs to issue an "apt-get update" command on each of the clients in a lab.
Try below:

Code:

#!/bin/bash
# Linux/UNIX box with ssh key based login
CLIENTS="192.168.1.1 192.168.1.2 192.168.1.3"
# SSH User name
USR="root"
# connect each client and run apt-get update
for host in $CLIENTS
do
ssh $USR@$host apt-get update
done


And about
Quote:

bash -c ssh server-name ls $DIR > /tmp/$NUM.txt
please elaborate

colucix 10-25-2010 04:32 AM

The problem is that the standard input of your local shell becomes the standard input of the remote command. This is the way ssh works. Every time the remote command terminates, ssh sends a SIGTTIN signal to the local shell, which results in terminating the standard input of the loop. To prevent this behaviour, you can try the -n option of ssh, that explicitly redirects the standard input from /dev/null, leaving the standard input of the local shell untouched.

kirecali 10-27-2010 05:41 AM

Quote:

Originally Posted by colucix (Post 4138370)
The problem is that the standard input of your local shell becomes the standard input of the remote command. This is the way ssh works. Every time the remote command terminates, ssh sends a SIGTTIN signal to the local shell, which results in terminating the standard input of the loop. To prevent this behaviour, you can try the -n option of ssh, that explicitly redirects the standard input from /dev/null, leaving the standard input of the local shell untouched.

This is exactly what I had needed. Thanks very much.


All times are GMT -5. The time now is 10:04 PM.