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