菜鸟笔记
提升您的技术认知

远程重启linux主机的几种方法

方法一,在终端上利用ssh,不登录远程主机,直接发送重启命令

ssh root@192.168.8.128 'reboot'

方法二,在终端上利用ssh和here document

ssh root@192.168.8.128 bash <<EOF
reboot
EOF

这种方式在reboot的位置其实可以执行多条其他的命令,说到这儿,还有一种有意思的用法

ssh root@192.168.8.128 bash < test.sh

这样可以在远程主机上执行本地写好的一个脚本文件

方法三,通过expect脚本自动执行,以下是我写的一个expect脚本,test.expect

#!/usr/bin/expect

set RHOST [lindex $argv 0]
set PASSWORD [lindex $argv 1]
set timeout 30

spawn ssh root@$RHOST 'reboot'

expect {
        "yes/no" {send "yes\r";exp_continue}
        "password" {send "$PASSWORD\r"}
        timeout {exit 1}
}
expect eof
exit 0

调用时,参数为远程主机IP和密码,如 expect test.expect 192.168.8.128 123

 附:在linux系统上重启远程windows系统

net rpc shutdown -r -f -I 192.168.8.156 -U administrator%123