Operating System - HP-UX
1830161 Members
2745 Online
109999 Solutions
New Discussion

While loop ends after SSH command

 
SOLVED
Go to solution
Marly Thomas
Occasional Contributor

While loop ends after SSH command

Hello o wizards & pharaohs,

You gracious prior help has brought me back again asking for divine guidance.

I have a â while loopâ from reading a simple text file which I use to send commands to remote servers via ssh.

The problem is that after the first iteration the loop ends. It won't end if I don't do an ssh call. Iâ ve tried this with simple echo commands and that works fine, goes through all lines in the text file.

# --------------------------------------------
Here is the text file:
# Simple Text File
appserver1 domain1
appserver1 domain2
appserver2 domain1
appserver2 domain2
â ¦
# -------------------------------------------
Script:
# Simple while loop on text file contents

cat text_file.txt | while read LINE
do

export srvr=$(cat $LINE|awk -F '{print $1}')
export domn=$(cat $LINE|awk -F '{print $2}')

ssh $srvr ps -ef|grep $domn

done
# -------------------------------------------

The first iteration always works. With the ssh command the loop simply ends and won't go to the next value in the while loop - no errors.

What the heck am I missing?

Thanks in advance,

LSD and UNIX are both from Berkley - 'nuf said.
7 REPLIES 7
RAC_1
Honored Contributor

Re: While loop ends after SSH command

Script:
# Simple while loop on text file contents

cat text_file.txt | while read LINE
do

export srvr=$(cat $LINE|awk -F '{print $1}')
export domn=$(cat $LINE|awk -F '{print $2}')

ssh $srvr "ps -ef|grep $domn"

done

OR

for i in `cat file`
do
ssh `export srvr=$(cat $LINE|awk -F '{print $1}')` "`export domn=$(cat $LINE|awk -F '{print $2}')`"
done

Anil
There is no substitute to HARDWORK
Abdul Rahiman
Esteemed Contributor

Re: While loop ends after SSH command

I had the same problem a while ago and the `for i" worked for me instead of the while do loop.

So the above solution should work.
for line in `cat text_file.txt`
do
srvr=`echo $line | awk '{print $1}'`
ssh $line "echo test"
done
No unix, no fun
Mark Grant
Honored Contributor

Re: While loop ends after SSH command

There definately used to be a bug in the posix shell that made while loops do this kind of thing but I thought it was sorted a long time ago.

However, you might try quoting your ssh command. I can't see how that pipe might break something but it might. Perhaps try

ssh $srvr "ps -ef|grep $domn". Obviously the grep will now be done on the remote site but I guess that doesn't matter too much.
Never preceed any demonstration with anything more predictive than "watch this"
Marly Thomas
Occasional Contributor

Re: While loop ends after SSH command

Thanks for the reply guys!

I solved this a bit differently, however. I couldn't use the for loop because it gave me the items in the text file either as a single string seperated by a space or the whole file in one line (using quotes).

So what I did was to submit the ssh command into the background - the output goes into a temp file anyway. So here is the result:

Here is the text file:
# Simple Text File
appserver1 domain1
appserver1 domain2
appserver2 domain1
appserver2 domain2
â ¦
-------------------------------------------
Script:

# Simple while loop on text file contents

cat text_file.txt | while read LINE
do

export srvr=$(cat $LINE|awk -F '{print $1}')
export domn=$(cat $LINE|awk -F '{print $2}')

ssh $srvr ps -ef|grep $dom >> /tmp/results.txt &

done
-----------------------------------------
LSD and UNIX are both from Berkley - 'nuf said.
Stuart Browne
Honored Contributor

Re: While loop ends after SSH command

What was occuring is that the STDIN was getting spat anywhere into the subshell created by the pipe.

So when you SSH'd in, the STDIN came open again, and sucked the rest of the contents of the 'cat'.

You can mess around with file-descriptors in order to get it to work properly, but backgrounding seems just as good a solution ;)
One long-haired git at your service...
Armin Kunaschik
Esteemed Contributor
Solution

Re: While loop ends after SSH command

Try running ssh with the -n switch.
This solves the problem with stdin.

Hope this helps,
Armin
And now for something completely different...
Marly Thomas
Occasional Contributor

Re: While loop ends after SSH command

Hello - here is a followup.

The backgrounding had some other side effects - I could only run so many at a time and many didn't return (I have 30 to run in total). So I was excited when I got in this morning to see some more responses to my problem.

The stdin explanation and the -n option is the coup de grace to the problem. I removed the backgrounding and put in the -n option and it worked great. Thanks!

Marly Thomas
LSD and UNIX are both from Berkley - 'nuf said.