您的位置:首页 > 运维架构 > Linux

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

2014-09-17 14:47 435 查看
方法一,在终端上利用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

至于echo+sleep+|的方式,作为一个菜鸟,后面有时间再研究下

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

net rpc shutdown -r -f -I 192.168.8.156 -U administrator%123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: