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?
# serverList
serverA
serverB
serverC
# 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
>
# 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.
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..
#!/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.
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..
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.
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 :-).
#!/bin/bash
while read server <&9; do
ssh $server uptime
done 9<zlist
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.
Found out that in bash "-u <filedescriptor>" is preferred way.. it needs space in between which I missed from Aia's post.
below code is also working now..
while read -u 9 server; do
ssh $server uptime
done 9<serverList