当前位置: > 虚拟化 Virtualization > 虚拟机 > VMware >

VM-ESXI 相关常用命令(Updateing)

时间:2019-09-03 19:06来源:linux.it.net.cn 作者:IT

# ESXI
计划任务路径:
cat /var/spool/cron/crontabs/root 
# 获取虚拟机列表
vim-cmd vmsvc/getallvms
获取vm状态
vim-cmd vmsvc/power.getstat [vmid]
关闭虚机
vim-cmd vmsvc/power.shutdown [vmid]
vim-cmd vmsvc/power.off [vmid] # 强制关闭
长期脚本存放路径
vi /etc/rc.local.d/local.sh # 内容写在exit 0 之前

# Esxi 开机脚本

#/bin/bash
# author:jerry
# create date:2017-8-29
# last update:2018-7-30
status=`vim-cmd /hostsvc/hostsummary | grep inMaintenanceMode|cut -c 27`
[ $status == "t" ] && vim-cmd hostsvc/maintenance_mode_exit 1&> /dev/null  
for on in `vim-cmd vmsvc/getallvms |awk '{print $1}'|tail -n 12`
do
if  [ $on -eq 11 ]
then
   continue
else
   vim-cmd vmsvc/power.on  $on
   sleep 30
fi
done

 

# Esxi 关机脚本

#!/bin/sh
# author:jerry
# create date:2017-7-27
# last update:2018-7-30
off=`esxcli vm process list|grep World |awk '{print $3}'|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  && sleep 10 && halt || halt
else
hostsum=`vim-cmd vmsvc/getallvms|awk 'NR>1{print $1}'|wc -l`
vim-cmd vmsvc/getallvms|awk 'NR>1{print $1}'>tmp.txt
 
 for i in `seq 1 $hostsum`
do 
vid=`sed -n "$i p" ./tmp.txt`
    on=`vim-cmd vmsvc/power.getstate $vid |tail -1|awk '{print $2}' 2&>/dev/null`
 if [ $on == on ] ;then
     vim-cmd vmsvc/power.shutdown $vid 1&>/dev/null
         sleep 5
 fi
   done
rm -rf ./tmp.txt
   
 off=`esxcli vm process list|grep World|awk '{print $3}'|wc -l`
 if [ $off == 0 ] ;then
      vim-cmd hostsvc/maintenance_mode_enter 1&> /dev/null
 sleep 10
   halt
    else
    ps |grep vmx|awk '{print $2}' |uniq  >tmp.txt   
    on=`ps |grep vmx|awk '{print $2}' |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
     sleep 10
 halt
  fi
fi

 

(责任编辑:IT)
------分隔线----------------------------