- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- While loop ends after SSH command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 03:46 AM
06-14-2004 03:46 AM
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,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 04:04 AM
06-14-2004 04:04 AM
Re: While loop ends after SSH command
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 04:13 AM
06-14-2004 04:13 AM
Re: While loop ends after SSH command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 04:35 AM
06-14-2004 04:35 AM
Re: While loop ends after SSH command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 05:07 AM
06-14-2004 05:07 AM
Re: While loop ends after SSH command
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
-----------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 11:44 AM
06-14-2004 11:44 AM
Re: While loop ends after SSH command
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 ;)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2004 08:48 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 02:48 AM
06-15-2004 02:48 AM
Re: While loop ends after SSH command
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