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?
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
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..
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.
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
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 :-).
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)