方法一:
脚本:
#!/bin/bash
if [ $# -lt 1 ]
then
echo "NO Args Input Error!!!"
exit
fi
case $1 in
"shutdown")
echo "===========shutdown now============"
ssh -t hadoop103 "echo \"123456\" | sudo -S shutdown -h now"
ssh -t hadoop104 "echo \"123456\" | sudo -S shutdown -h now"
ssh -t hadoop102 "echo \"123456\" | sudo -S shutdown -h now"
;;
"reboot")
echo "=========reboot============="
ssh -t hadoop103 "echo \"123456\" | sudo -S reboot"
ssh -t hadoop104 "echo \"123456\" | sudo -S reboot"
ssh -t hadoop102 "echo \"123456\" | sudo -S reboot"
;;
*)
echo "INput args Error!!"
;;
esac
参数说明:
hadoop10x:就是你的主机名。这里要求之前已经配好了ssh
123456:是你的密码。因为关机需要输入密码,所以提前给出密码,关机后就不需要输入。注意:启动脚本的服务器应该最后关闭和重启(先关闭其它虚拟机,然后关闭当前虚拟机)
方法二:更多服务器关闭使用
脚本:
for ip in `seq 103 104`;
do echo"---------[shutdown hadoop$ip]------------"
ssh hadoop$ip "echo \"123456\" | sudo -S shutdown -h now";
done
ssh hadoop102 "echo \"123456\" | sudo -S shutdown -h now"