Linux
重新啟動然後 ssh 到遠端機器
我想編寫一個 bash 腳本,通過 ssh 在遠端伺服器上執行一些命令。然後我需要重新啟動遠端伺服器,通過 ssh 再次連接並執行更多命令。
我正在使用這段程式碼:
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....'
問題是有時伺服器在我可以 ssh 到它之前返回 ping,然後腳本失敗。我的一些伺服器需要超過 45 秒的睡眠時間。有沒有其他方法可以做到這一點?例如,嘗試在 while 循環中使用 ssh 而不是 ping?
謝謝,
試試這個命令“sticky ssh”:
while true; do command ssh "$@"; [ $? -eq 0 ] && break || sleep 0.5; done
取自: http ://backreference.org/2013/04/26/ssh-auto-reconnect/
ls -l
根據您的執行級別/etc/rc3.d
或/etc/rc5.d
根據您的執行級別執行操作。這裡要注意的是網路守護程序在 ssh 守護程序之前啟動。因此ping
之前可用ssh
。在遠端機器上執行之前,您可能應該wait
多做一點。ssh
就我而言,大約有 19 個服務在網路之後但在 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