
# get all the VMs identifiers
VMID=$(/usr/bin/vim-cmd vmsvc/getallvms | grep -v Vmid | awk '{print $1}')
# loop through all the VMs
for i in $VMID
do
# get their state(turned on,off,whatever)
STATE=$(/usr/bin/vim-cmd vmsvc/powergetstate $i | tail -1 | awk '{print $2}')
# if they are running,turn them off (only works correctly if
# vmware tools are installed on the VMs)
if [ $STATE == on ]
then
/usr/bin/vim-cmd vmsvc/powershutdown $i
fi
done
#shutdown the host itself
sleep 30
/sbin/shutdownsh
/sbin/poweroff
实际也可以通过运行命令
esxcli system maintenanceMode set -e y
进入维护模式maintenanceMode(Mode的M为大写)
esxcli system shutdown poweroff -d 60 -r test
关机
问题是虚拟机必须关闭了才能进入维护模式,又回到用脚本来自动关闭虚拟机的问题上来了。安装sudo软件包后,用vi查看/etc/sudoers的内容(需要以root身份进行),将需要使用sudo命令的用户(或组)加入到该配置文件里,格式很简单,相信你看看里面的例子和说明就明白了。一般使用sudo时会提示输入当前用户的密码(防止别人偷用),如果你嫌这样麻烦,也可以在/etc/sudoers里面设置的地方加入 :NOPASSWD 参数,这样就可以直接使用而不需要输入密码了。另外,如果你的机器就一个人使用,也可以将 /sbin/shutdown 的属性改成 4755,就是 chmod 4755 /sbin/shutdown,然后不用安装任何别的软件了,任何用户都可以直接用这个命令关机(注意:这可能是一个安全隐患 :)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)