LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2016, 05:21 PM   #1
aggrishabh
Member
 
Registered: Nov 2010
Posts: 87

Rep: Reputation: 1
ssh hang in shell script


Hi All,

i have a script which is doing ssh to many servers in for loop and execute sed command.

sometime sed hangs and so the whole script.

sed -i s:^password.*:"password --md5 NEW_PASS": /boot/grub/grub.conf

above sed command is working fine for most of the hosts but hangs for some specially RHEL5. please suggest if something is wrong with the command.
Also is there any way we can move to next serer in list if command fail to execute in 5 or 10 sec without user intervention.
 
Old 05-20-2016, 08:18 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
you could use 'screen' (if available) to detach from current terminal so the commands are executed in background somewhat.

Code:
for server in server1 server2 server3; do
  ssh $server screen -d -m sed -i 's/^password.*/password --md5 NEW_PASS/' /boot/grub/grub.conf
done

Last edited by keefaz; 05-20-2016 at 08:36 PM. Reason: added detach screen switches -d -m
 
Old 05-20-2016, 11:52 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by aggrishabh View Post
Also is there any way we can move to next serer in list if command fail to execute in 5 or 10 sec without user intervention.
ssh will pass on the exit code of the remote program, but running screen remotely will eat that though it might allow time for the program to complete, if time is what it needs.

Keefaz' loop could be modified to use 'timeout'

Code:
for server in server1 server2 server3; do
  ssh $server timeout -k 10 5 sed -i 's/^password.*/password --md5 NEW_PASS/' /boot/grub/grub.conf
  echo Status was $?
done
That would send a TERM signal after 5 seconds to 'sed' if it is still running. Then if that didn't work it would send a KILL signal after 10 seconds. But if 'sed' ran to a natural completion its return code would be passed to the original shell, the one running the loop. If 'timeout' stops 'sed' then the exit code will be 124 or 137
 
Old 05-21-2016, 08:51 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by aggrishabh View Post
Hi All,

i have a script which is doing ssh to many servers in for loop and execute sed command.

sometime sed hangs and so the whole script.

sed -i s:^password.*:"password --md5 NEW_PASS": /boot/grub/grub.conf

above sed command is working fine for most of the hosts but hangs for some specially RHEL5. please suggest if something is wrong with the command.
Also is there any way we can move to next serer in list if command fail to execute in 5 or 10 sec without user intervention.
I'm not certain of this, but in some cases a system may need a terminal attached for it work properly. You might
try using
Code:
ssh -t $server <rest of your command>
Next, I would suggest updating those RH5 systems to something newer.

Last edited by jpollard; 05-21-2016 at 08:53 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using ssh to execute dozens of shell commands in shell script hilou Programming 7 04-20-2016 04:25 AM
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 03:23 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration