由于近期大厦需要维护,因此通知晚上需要断电,因此写了一个针对ESXI主机自动关机的脚本,时间仓促,脚本还有许多可以改进的地方,勿喷。

具体脚本如下:

#/bin/bash
off=`esxcli vm process list|grep World |cut -c 13- |wc -l`
status=`vim-cmd /hostsvc/hostsummary | grep inMaintenanceMode|cut -c 27`
if [ $off == 0 ] ;then
[ $status == "f" ] && vim-cmd hostsvc/maintenance_mode_enter 1&> /dev/null  && halt || halt
else
esxcli vm process list|grep World |cut -c 13- >tmp.txt
on=`esxcli vm process list|grep World |cut -c 13- |wc -l`
for i in `seq 1 $on`
  do
hostid=`sed -n " $i p " ./tmp.txt`
esxcli vm process kill -t soft -w $hostid 1&> /dev/null
sleep 3
done
rm -rf ./tmp.txt
off=`esxcli vm process list|grep World |cut -c 13- |wc -l`
 if [ $off == 0 ] ;then
  vim-cmd hostsvc/maintenance_mode_enter 1&> /dev/null
  halt
 else
  ps |grep vmx|cut -d" "  -f3 |uniq  >tmp.txt   
  on=` ps |grep vmx|cut -d" "  -f3 |uniq |wc -l`
  for i in `seq 1 $on`         
    do                                
    hostid=`sed -n " $i p " ./tmp.txt`  
    kill $hostid 1&> /dev/null
   done            
    rm -rf ./tmp.txt
    vim-cmd hostsvc/maintenance_mode_enter 1&> /dev/null
    halt
 fi
fi

写好脚本后,设置为可执行权限,添加到ESXI的计划任务程序中即可。

ESXI计划任务路径: vi /var/spool/cron/crontabs/root

wKioL1mTkUaxpxRaAAASahD-ZAM073.png

注意:ESXI中除了数据存储目录,其它目录创建的文件,重启后自动丢失,因此最好把脚本放存储目录下:/vmfs/volumes/{安装ESXI时所取的存储器名字}

wKioL1mTlgqhASojAAAkDWB5J-I810.png

更新优化版:

#!/bin/sh          
#  author: jerry   
#  create:2017-8-16                    
#  update:2020-5-28                                
logs='/vmfs/volumes/datastore1/log.txt'            
Vmid=`vim-cmd vmsvc/getallvms|awk 'NR>1{print $1}'`
for id in  $Vmid ;do                                                       
    timer=`date +%F_%T`                                                   
    Vmstate=`vim-cmd vmsvc/power.getstate $id |tail -1|awk '{print $2}'`                         
    if [ "$Vmstate" == "on" ] ;then                                                                
       vim-cmd vmsvc/power.shutdown $id && echo "Vmid $id  At  $timer Closed Successed. " >> $logs
    fi                                                        
done                                                          
sleep 180
off=`esxcli vm process list|grep World|awk '{print $3}'|wc -l`
if [ $off == 0 ] ;then                                
   vim-cmd hostsvc/maintenance_mode_enter &> /dev/null
   sleep 10
   halt                                               
else                                                  
   Vmid=`vim-cmd vmsvc/getallvms|awk 'NR>1{print $1}'`
   for id in  $Vmid ;do                                                       
       timer=`date +%F_%T`                                                   
       Vmstate=`vim-cmd vmsvc/power.getstate $id |tail -1|awk '{print $2}'`                                    
       if [ "$Vmstate" == "on" ] ;then                                                                           
          vim-cmd vmsvc/power.off $id && echo "Vmid $id  At  $timer By Admin  Force Closed Successed. " >> $logs
       fi                                                        
   done                                                          
   sleep 120
   off=`esxcli vm process list|grep World|awk '{print $3}'|wc -l`
   if [ $off == 0 ] ;then                                 
       vim-cmd hostsvc/maintenance_mode_enter &> /dev/null
       sleep 10
       halt                                       
   else                                           
       VmPid=`ps |grep vmx|awk '{print $2}' |uniq`
       for pid in $VmPid ;do                                                                 
          timer=`date +%F_%T`                                                                
          kill $pid 1&> /dev/null && echo "Vmid Pid $pid  killed  By Admin  At $timer. " >> $logs
       done                                               
       vim-cmd hostsvc/maintenance_mode_enter &> /dev/null
       sleep 10
       halt
   fi
fi