1

I want to write a bash script that performs some commands on a remote server via ssh. Then I need to reboot the remote server, connect again via ssh and perform more commands.

I'm using this code:

    ssh $theip 'sudo reboot'
    echo "starting sleep---1"
        sleep 30
        echo " finished sleeping1"
        while ! ping -c 1 $theip &>/dev/null; do echo "waiting..."; done
        echo "starting sleep---2"
        sleep 45
        echo " finished sleeping2"
        echo "finished rebooting"
ssh $theip 'commands....'

The problem is that sometimes the server is returning pings before I can ssh to it and then the script fails. Some of my servers need more than the 45s sleep. Is there any other method to do this? For example, try to ssh in the while loop instead of ping?

Thanks,

  • Check for port 22 being open in a loop using a utility like Netcat (aka nc). When port 22 on the remote host is open, wait a few more seconds and then ssh in and run the remote commands. On a related note, look into Ansible. It's an awesome tool for remote management that doesn't require agents running on the remote servers. – Deathgrip Sep 19 '17 at 15:48
  • Ping will respond during the POST on some devices. It could take minutes after post before sshd is run. Don't rely on time. You should certainly begin with using ssh in your loop instead of ping. You could also do it in steps, First loop until ping and then loop until ssh. Set a timer and give a maximum timeout for the whole thing to avoid endless loops. – HackSlash Sep 19 '17 at 15:49
1

Try this command for "sticky ssh":

while true; do command ssh "$@"; [ $? -eq 0 ] && break || sleep 0.5; done

Taken from: http://backreference.org/2013/04/26/ssh-auto-reconnect/

  • The [ $? -eq 0 ] looks superfluous. Would it not be enough with ssh ... && break || sleep 1? – Kusalananda Sep 19 '17 at 15:55
  • 1
    Or even simpler while ! ssh "$@"; do sleep 0.5; done – Michael Daffin Sep 19 '17 at 17:12
  • The idea is: keep reconnecting as long as the ssh exit status is non-zero. – HackSlash Sep 19 '17 at 19:37
  • Thanks, 2 followup questions: 1.How do I make the ssh connecting attempts not visible? 2. After the machine is up, it just stays in the machine and does not perform the next commands. After I exit manually from the machine it performs the next commands on my local machine form which I ran the script. – lakerda Sep 24 '17 at 8:08
  • First, you could send output to the gaping black hole of doom with "> /dev/null 2>&1" second question is unclear. – HackSlash Sep 25 '17 at 15:38
0

Do an ls -l on your /etc/rc3.d or /etc/rc5.d depending on your runlevel. The thing to note here is that the network daemon starts before the ssh daemon. therefore ping is available before ssh. You should probably wait a bit more before executing ssh on the remote machine. In my case there are around 19 services that are started after network but before ssh.

lrwxrwxrwx 1 root root 17 Sep  9  2011 S10network -> ../init.d/network
lrwxrwxrwx 1 root root 16 Sep  9  2011 S11auditd -> ../init.d/auditd
lrwxrwxrwx 1 root root 21 Sep  9  2011 S12restorecond -> ../init.d/restorecond
lrwxrwxrwx 1 root root 16 Sep  9  2011 S12syslog -> ../init.d/syslog
lrwxrwxrwx 1 root root 20 Sep  9  2011 S13irqbalance -> ../init.d/irqbalance
lrwxrwxrwx 1 root root 19 Sep  9  2011 S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx 1 root root 19 Sep  9  2011 S18rpcidmapd -> ../init.d/rpcidmapd
lrwxrwxrwx 1 root root 17 Sep  9  2011 S19rpcgssd -> ../init.d/rpcgssd
lrwxrwxrwx 1 root root 15 Sep  9  2011 S20kdump -> ../init.d/kdump
lrwxrwxrwx 1 root root 20 Sep  9  2011 S22messagebus -> ../init.d/messagebus
lrwxrwxrwx 1 root root 24 Sep  9  2011 S23setroubleshoot -> ../init.d/setroubleshoot
lrwxrwxrwx 1 root root 15 Sep  9  2011 S25netfs -> ../init.d/netfs
lrwxrwxrwx 1 root root 15 Sep  9  2011 S25pcscd -> ../init.d/pcscd
lrwxrwxrwx 1 root root 15 Sep  9  2011 S26acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 19 Sep  9  2011 S26haldaemon -> ../init.d/haldaemon
lrwxrwxrwx 1 root root 14 Sep  9  2011 S26hidd -> ../init.d/hidd
lrwxrwxrwx 1 root root 20 Sep  9  2011 S26lm_sensors -> ../init.d/lm_sensors
lrwxrwxrwx 1 root root 16 Sep  9  2011 S28autofs -> ../init.d/autofs
lrwxrwxrwx 1 root root 15 Sep  9  2011 S50hplip -> ../init.d/hplip
lrwxrwxrwx 1 root root 15 Sep 13  2011 S50snmpd -> ../init.d/snmpd
lrwxrwxrwx 1 root root 14 Sep  9  2011 S55sshd -> ../init.d/sshd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.