全手工打造ESXI6.0 自动关闭虚拟服务器及ESXI主机脚本

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

具体脚本以下:bash

#/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
ide

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

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

wKioL1mTkUaxpxRaAAASahD-ZAM073.png

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

wKioL1mTlgqhASojAAAkDWB5J-I810.png

更新优化版:crontab

#!/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
figet

相关文章
相关标签/搜索