Visit Our UNIX and Linux User Community


While loop is causing ssh command to exit from script after first iteration.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop is causing ssh command to exit from script after first iteration.
# 1  
Old 07-13-2015
While loop is causing ssh command to exit from script after first iteration.

I am trying to check multiple server's "uptime" in a loop over "ssh".
When I execute multiple ssh commands with hard coded servernames script is executing fine.

But when I pass server names using while loop, script is exiting after checking first server's status, why?

Code:
# serverList
serverA
serverB
serverC

Code:
# checkServerStatus.sh
#!/bin/bash

# This block got executed fine without exiting from script.
ssh serverA uptime
ssh serverB uptime
ssh serverC uptime

# This block is exiting from script after executing ssh once with serverA.
while read server; do
        ssh $server uptime
done < serverList

Code:
> 
# output 
$> bash -xv ./test.sh
#!/bin/bash

ssh serverA uptime
+ ssh serverA uptime
 04:38am  up 39 days  0:23,  1 user,  load average: 0.00, 0.02, 0.05
ssh serverB uptime
+ ssh serverB uptime
 04:38am  up 38 days 22:40,  0 users,  load average: 0.01, 0.04, 0.05
ssh serverC uptime
+ ssh serverC uptime
 04:38am  up 39 days  5:47,  0 users,  load average: 0.00, 0.01, 0.05


while read server; do
        ssh $server uptime
done <serverList
+ read server
+ ssh serverA uptime
 04:38am  up 39 days  0:23,  1 user,  load average: 0.00, 0.02, 0.05
+ read server

If I comment "ssh $server uptime" and simply print it "echo ssh $server uptime", while loop is not exiting.

How can I fix this or work around this?

Last edited by kchinnam; 07-13-2015 at 01:57 AM.. Reason: indentation
# 2  
Old 07-13-2015
From your description, one might guess that both read and ssh are reading text from serverList (although it isn't obvious why).

Try changing:
Code:
        ssh $server uptime

to:
Code:
        ssh $server uptime < /dev/null

# 3  
Old 07-13-2015
From man ssh
Quote:
-n Redirects stdin from /dev/null (actually, prevents reading from stdin).
Code:
while read server; do
        ssh -n $server uptime
done <serverList

That would work as long as ssh is not expecting a password or paraphrase.

Since you are using bash, you can as well change the file descriptor that the read will read from.
Code:
while read -u12 server; do
        ssh $server uptime
done 12<serverList


Last edited by rbatte1; 07-13-2015 at 09:48 AM.. Reason: Quote tags
# 4  
Old 07-13-2015
Don, usually I would be scared to try </dev/null(empty stuff), but I tried, its working,, how is it telling ssh not to use same file descriptor to read serverList?

Aia, changing file descriptor is not working for me..
Code:
#!/bin/bash
while read -u12 server; do
        ssh $server uptime
done 12 < serverList

output:
./test.sh: line 4: syntax error near unexpected token `12'

ssh -n is working for my current script. But I do see myself using ssh in a interactive manner and enter passwords to remote server login.. in that case I guess while can't be an option..

Interestingly when I replaced while with for loop, its working fine.. since my input is simple I can afford, but this is not the right way for complex inputs.

Code:
for server in $(cat $serverList); do
        ssh $server uptime
done

How can tell ssh not to read from serverList file? -u12 trick is not working on SuSE linux 11 I am using..

Last edited by kchinnam; 07-13-2015 at 05:07 AM.. Reason: clarifying
# 5  
Old 07-13-2015
ssh - unless told otherwise - reads from stdin and empties the serverlist until EOF when used within the while loop. A for loop doesn't access stdin; there's the difference.
If you want to have interactive input as well as file input, you'll have to separate both streams, as Aia proposed using a different input file descriptor for the serverlist.
# 6  
Old 07-13-2015
How can I separate file descriptor only for ssh without using "-n"?
I am using SuSE linux 11, "while read -u12 server; do" trick is not working.
I also tried "while read server <&10; do", its not working either.

---------- Post updated at 04:20 AM ---------- Previous update was at 04:13 AM ----------

I would propose this solution. Use a separate file descriptor for "while read", I picked 9 here(when for is not a choice :-).

Code:
#!/bin/bash
while read server <&9; do
        ssh $server uptime
done 9<zlist

# 7  
Old 07-13-2015
The 12< trick IS working, as long as you comply the the correct syntax (no space!).
And, <&12 would duplicate stdin from fd 12 which is assumedly unassigned and thus "doesn't work". Had you assigned fd 12 to some file prior to using it, it would have worked.

Previous Thread | Next Thread
Test Your Knowledge in Computers #817
Difficulty: Medium
In CSS, E[foo="bar"] matches an E element whose "foo" attribute value is exactly equal to "bar".
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop is running only for the first iteration

Hello, I've written a script to automate encoding of all the MP4 files in a directory (incl. subdirectories). But unfortunately it's running for the first MP4 file only. My machine details: root@Ubuntu16:~# uname -a Linux Ubuntu16 4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48... (2 Replies)
Discussion started by: prvnrk
2 Replies

2. Shell Programming and Scripting

Loop iteration with two variables

Hello, I have been stuck on this for some time and invested many hours trying to find a solution. I am trying to either loop through two variables or or two arrays and not sure how to do it. I am limited to ksh only, and don't have the ability to do a foreach, or for i AND for j etc...I... (19 Replies)
Discussion started by: Decoy Octopus88
19 Replies

3. Shell Programming and Scripting

Getting the iteration count in WHILE LOOP

bash in RHEL 6.4 I have a requirement in which I want to get the iteration count from a WHILE LOOP. The below mentioned simple script test.sh works fine. In the below script, the WHILE loop will iterate every 5 seconds infinitely until it greps the string BASKETBALL from /tmp/somestring.txt... (6 Replies)
Discussion started by: John K
6 Replies

4. Programming

String array iteration causing segfault

I am populating an array of string and print it. But it going in infinite loop and causing segfault. char Name = { "yahoo", "rediff", "facebook", NULL }; main(int argc, char* argv) { int j = 0; ... (7 Replies)
Discussion started by: rupeshkp728
7 Replies

5. Shell Programming and Scripting

Do something only that last iteration of loop

I have a script with logic like: my_function() { if mkdir $1 mkdir mydir_${2} else do something else fi } read in list of items while read list do my_function $list `date` done so basically it will make a directory for every name in the list and create a directory with the... (6 Replies)
Discussion started by: glev2005
6 Replies

6. Shell Programming and Scripting

while loop stops after first iteration - remote ssh exit command problem?

I have written the following script to update some Debian boxes. #!/bin/bash mxg_hosts_file="/etc/mxg/ssh-hosts" while read line ; do mxg_host="$(echo ${line} | awk -F":" '{print $1}')" mxg_port="$(echo ${line} | awk -F":" '{print $2}')" echo "Connecting and Upgrading... (3 Replies)
Discussion started by: jelloir
3 Replies

7. Shell Programming and Scripting

howto stop loop iteration

I wonder how to stop further loop iterations when conditions gets false e.g. This file.txt contains the following structure : 1 2 3 4 5 6 7 8 9 10 How to stop iteration when if statement gets false ? for n in `cat file.txt` do if (( n<=5 )) (1 Reply)
Discussion started by: presul
1 Replies

8. UNIX for Advanced & Expert Users

Multiple ssh, shutdown and exit script

PROBLEM: I need to ssh into multiple remote machines, send a shutdown command and exit. The remote servers will then run their own scripts to gracefully shutdown their applications before shutting down. ONE: This is to be achieved without using public key authentication; this is being avoided... (2 Replies)
Discussion started by: ulemsee
2 Replies

9. Shell Programming and Scripting

New iteration of for-loop without incrementing?

Another question, is it possible to, in a for-loop incrementing until it reaches a certain number, to have it loop again without incrementing? Just have it drop what it is doing when it reaches this command and start again at the same number it was at? I know I could make a while loop and just... (0 Replies)
Discussion started by: jeriryan87
0 Replies

10. Solaris

SSH doesnt exit properly from command line

I ssh to another server and run a few commands - start a few processes that run on the server. I then type exit - by my command line hangs. I have heard that it is waiting until anything processes you are running complete, but these processes are going to run 24*7*365 so obviously I cant wait.... (4 Replies)
Discussion started by: frustrated1
4 Replies

Featured Tech Videos