while loop stops after first iteration - remote ssh exit command problem?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting while loop stops after first iteration - remote ssh exit command problem?
# 1  
Old 02-09-2012
while loop stops after first iteration - remote ssh exit command problem?

I have written the following script to update some Debian boxes.

Code:
#!/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 ${mxg_host}"
    ssh root@${mxg_host} -p ${mxg_port} 'aptitude update && aptitude safe-upgrade -y && reboot && exit'
done < ${mxg_hosts_file}

Code:
> cat /etc/mxg/ssh-hosts
mx.example1.com:2225
mail.example2.com:22
mail.example3.com:222

The while loops stops after the first loop - it almost seems like the exit in the remote ssh command is being interpreted by the while loop, but I am not sure.

Code:
> bash -xv /usr/local/bin/updatemxg.sh
#!/bin/bash

mxg_hosts_file="/etc/mxg/ssh-hosts"
+ mxg_hosts_file=/etc/mxg/ssh-hosts
#mxg_hosts_file="/etc/mxg/ssh-hosts.test"

while read line ; do
    mxg_host="$(echo ${line} | awk -F":" '{print $1}')"
    mxg_port="$(echo ${line} | awk -F":" '{print $2}')"
        echo "Connecting and Upgrading ${mxg_host}"
        ssh root@${mxg_host} -p ${mxg_port} 'aptitude update && aptitude safe-upgrade -y && reboot && exit'
done < ${mxg_hosts_file}
+ read line
echo ${line} | awk -F":" '{print $1}')"
echo ${line} | awk -F":" '{print $1}')
echo ${line} | awk -F":" '{print $1}'
++ awk -F: '{print $1}'
++ echo mx.example1.com:2225
+ mxg_host=mx.example1.com
echo ${line} | awk -F":" '{print $2}')"
echo ${line} | awk -F":" '{print $2}')
echo ${line} | awk -F":" '{print $2}'
++ awk -F: '{print $2}'
++ echo mx.example1.com:2225
+ mxg_port=2225
+ echo 'Connecting and Upgrading mx.example1.com'
Connecting and Upgrading mx.example1.com
+ ssh root@mx.example1.com -p 2225 'aptitude update && aptitude safe-upgrade -y && reboot && exit'
Hit http://mirror.aarnet.edu.au lenny Release.gpg
Ign http://mirror.aarnet.edu.au lenny/main Translation-en_AU
..... truncated for readability of post ....
Hit http://volatile.debian.org lenny/volatile/non-free Sources
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
Reading extended state information...
Initializing package states...
Reading task descriptions...
No packages will be installed, upgraded, or removed.
0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B of archives. After unpacking 0B will be used.
Reading package lists...
Building dependency tree...
Reading state information...
Reading extended state information...
Initializing package states...
Reading task descriptions...
+ read line
>

If I remove the SSH line it works:

Code:
> /usr/local/bin/updatemxg.sh
Connecting and Upgrading mx.example1.com
Connecting and Upgrading mail.example2.com
Connecting and Upgrading mail.example3.com
>

If the remote ssh exit is indeed causing the while loop to stop how would I prevent that happening
# 2  
Old 02-09-2012
You really do not need to exit since it is going to reboot. Alternatively try to use shutdown with a delay (say 1 minute)
Code:
'aptitude update && aptitude safe-upgrade -y && shutdown -r +1

BTW, there is a simpler and efficient way to pass the colon separated files to read using IFS (internal field separator). Below code tells read to change the default IFS (which is white space) to colon.
Code:
while IFS=":" read mxg_host mxg_port
do
  echo $mxg_host
  echo $mxg_port
done < /etc/mxg/ssh-hosts

# 3  
Old 02-09-2012
Thanks chihung, I tried your suggestion but it still fails, so the exit doesn't appear to be the problem. If I check the return value from the ssh command it is 0, so i'm stumped? Thanks for the IFS suggestion.

The script currently looks like this:

Code:
#!/bin/bash

mxg_hosts_file="/etc/mxg/ssh-hosts"

while IFS=":" read mxg_host mxg_port ; do
    echo "Connecting and Upgrading ${mxg_host}"
    ssh root@${mxg_host} -p ${mxg_port} 'aptitude update && aptitude safe-upgrade -y && shutdown -r now'
done < ${mxg_hosts_file}

Edit - figured it out.

You have to use ssh's -n argument.

Code:
 -n      Redirects stdin from /dev/null (actually, prevents reading from
             stdin).  This must be used when ssh is run in the background.  A
             common trick is to use this to run X11 programs on a remote
             machine.  For example, ssh -n shadows.cs.hut.fi emacs & will
             start an emacs on shadows.cs.hut.fi, and the X11 connection will
             be automatically forwarded over an encrypted channel.  The ssh
             program will be put in the background.  (This does not work if
             ssh needs to ask for a password or passphrase; see also the -f
             option.)


Last edited by jelloir; 02-09-2012 at 10:03 PM..
# 4  
Old 02-10-2012
Please have a look at this: Shell Loop Interaction with SSH
A better solution is to use the for loop
Code:
for line in $(cat ${mxg_hosts_file}); do
    echo "Connecting and Upgrading ${line%%:*}"
    ssh root@${line%%:*} -p ${line##*:} "bash -s" < command_to_run
done

command_to_run is a script that you intended to execute on the remote boxes
Code:
aptitude update 
aptitude safe-upgrade -y
shutdown -r now

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Declare and grep a variable via ssh/remote/loop

If I am running a bash command, and some awk getting the ethernet adapter on the local machine. It works fine. But if I will run it from the remote, it is EMPTY on echo and throwing error in grep. Thank you This work perfectly fine $ f=`/sbin/ip a|grep 127.127 | awk '{print $NF }' ` ; ip... (2 Replies)
Discussion started by: kenshinhimura
2 Replies

2. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

3. Shell Programming and Scripting

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? # serverList... (8 Replies)
Discussion started by: kchinnam
8 Replies

4. Shell Programming and Scripting

Input to while loop in remote machine using ssh

Hello I am writing a script in a local machine, i am using ssh, here i am not able to using back ticks & input file to while loop. here is my script ssh -q server1 a=`ps -ef | grep ccms | awk {print $1}` b=`ps -ef | grep mss | awk {print $1}` # above lines are not working, so i redirected... (12 Replies)
Discussion started by: nanz143
12 Replies

5. Shell Programming and Scripting

Ssh to remote server loop is breaking

hi All, cat login.list server1 userid1 server2 userid2 server3 userid3 ---------------------------------------- #SSHSCRIPT.ksh FILE=login.list while read vah vah_id do ssh $vah -l $vah_id "pwd" done < "$FILE" ----------------------------------------- When i... (2 Replies)
Discussion started by: raghur77
2 Replies

6. HP-UX

[Solved] ssh debug1: Exit status 254 problem

Hello; Am experiencing odd problem with ssh: ========= ssh -vvv remote_host : : debug2: channel 0: rcvd adjust 65536 debug2: channel_input_status_confirm: type 99 id 0 debug2: shell request accepted on channel 0 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0... (4 Replies)
Discussion started by: delphys
4 Replies

7. Shell Programming and Scripting

Expect script: obtain the exit code of remote command

Hi all, I'm trying to run the sipp simulator in crontab but after some attempt I came to the conclusion that for some reason this isn't possible (maybe due to sipp interactive nature). This is confirmed by these posts. Now I'm trying to launch sipp from an expect script that runs in crontab. ... (0 Replies)
Discussion started by: Evan
0 Replies

8. Shell Programming and Scripting

ssh stops a while loop

The while loop exits (early) when a simple ssh command is run. #!/bin/ksh #set -x #------------------------------------------------------------------------- # Functions Section #------------------------------------------------------------------------- while : do cat list.txt|while read... (1 Reply)
Discussion started by: bkdias26
1 Replies

9. Shell Programming and Scripting

Getting the exit status of a remote command

Hi to everyone. How can I get the exit status from a remote command executed with rexec? :eek: machine A has RedHat Linux 9 and the remote machine B has SCO UNIX. Code: rexec -l user -p password host sh /u/files/scripts/seq_cal.sh 2006 08 I want the exit status returned by... (1 Reply)
Discussion started by: zoonalex
1 Replies

10. Solaris

SSH doesnt exit properly from command line

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)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question