#!/bin/bash
#Author:keme
#Blog:https://www.cnblogs.com/keme/
#Time:2018-11-23
#Name:auto_vm.sh
#Version:V1.0
PATH_TEM="/E/VMS/Centos7/template/template.vmx"
PATH_SERVICE="/E/VMS/Centos7"
PATH_SERVICE_CMD="E:\VMS\Centos7"
#######################################
#PATH_TEM="/E/VMS/Debian8/template/template.vmx"
#PATH_SERVICE="/E/VMS/Debian8"
#PATH_SERVICE_CMD="E:\\VMS\\Debian8"
######################################
PATH_VMRUN_CMD="E:\\SOFT\\VM"
PATH_VMS="E:\\VMS"
SERVER_NAME="FDSF"
CLUSTER_NAME="FDFS-cluster"
SHOT_NAME="$2"
# 创建克隆链接的虚拟机
VM_create(){
for i in $(cat host-list.txt)
do
echo "开始创建链接克隆: ${SERVER_NAME}-${i}"
/E/SOFT/VM/vmrun.exe -T ws clone "${PATH_TEM}" ${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx linked -snapshot=CLEAN -cloneName=${SERVER_NAME}-${i}
sleep 2
done
}
# 启动创建好的虚拟机
VM_start(){
for i in $(cat host-list.txt)
do
echo "开始启动: ${SERVER_NAME}-${i}"
/E/SOFT/VM/vmrun.exe -T ws start ${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx
sleep 10
done
}
# 批量修改IP地址
VM_set_IP(){
> ${PATH_VMS}\\set_ip.bat
for i in $(cat host-list.txt)
do
echo "${PATH_VMRUN_CMD}\\vmrun.exe -T ws -gu root -gp 123456 runProgramInGuest \"${PATH_SERVICE_CMD}\\${CLUSTER_NAME}\\${SERVER_NAME}-${i}\\${SERVER_NAME}-${i}.vmx\" /bin/bash /root/first.sh ${i} ${SERVER_NAME}-${i}" >> ${PATH_VMS}\\set_ip.bat
echo "ping -n 5 127.0.0.1 >nul" >> ${PATH_VMS}\\set_ip.bat
done
echo "修改IP脚本生成路径: ${PATH_VMS}\\set_ip.bat"
echo -e "运行命令:\n1.打开cmd.exe\n2.输入: ${PATH_VMS}\\set_ip.bat"
}
# 修改好后批量关机
VM_stop(){
for i in $(cat host-list.txt)
do
echo "开始关闭: ${SERVER_NAME}-${i}"
/E/SOFT/VM/vmrun.exe -T ws stop ${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx
sleep 2
done
}
# 关机后批量创建快照
VM_snapshot(){
for i in $(cat host-list.txt)
do
echo "开始快照: ${SERVER_NAME}-${i}"
/E/SOFT/VM/vmrun.exe -T ws snapshot "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx" ${SHOT_NAME}
sleep 2
done
}
# 获取快照列表
VM_shot_list(){
for i in $(cat host-list.txt)
do
echo "获取快照列表: ${SERVER_NAME}-${i}"
/E/SOFT/VM/vmrun.exe -T ws listSnapshots "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx"
sleep 2
done
}
# 批量恢复快照
VM_shot_back(){
for i in $(cat host-list.txt)
do
echo "开始恢复快照: ${SERVER_NAME}-${i} ${SHOT_NAME}"
/E/SOFT/VM/vmrun.exe -T ws revertToSnapshot "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx" ${SHOT_NAME}
sleep 2
done
}
# 批量删除快照
VM_shot_del(){
for i in $(cat host-list.txt)
do
echo "开始删除快照: ${SERVER_NAME}-${i} ${SHOT_NAME}"
/E/SOFT/VM/vmrun.exe -T ws deleteSnapshot "${PATH_SERVICE}/${CLUSTER_NAME}/${SERVER_NAME}-${i}/${SERVER_NAME}-${i}.vmx" ${SHOT_NAME}
sleep 2
done
}
# 获取当前正在运行的虚拟机
VM_list(){
echo "当前正在运行的虚拟机:"
/E/SOFT/VM/vmrun.exe -T ws list
}
case $1 in
create)
VM_create
;;
start)
VM_start
;;
stop)
VM_stop
;;
shot)
VM_snapshot
;;
set_ip)
VM_set_IP
;;
shot_back)
VM_shot_back
;;
shot_del)
VM_shot_del
;;
shot_list)
VM_shot_list
;;
list)
VM_list
;;
*)
echo "bash $0 {create|start|set_ip|stop|shot|shot_list|shot_back|shot_del|list}"
esac
除了 set_ip 这个函数在windows 里面需要单独拿出来放到cmd 窗口使用
host-list.txt表示创建虚拟机的数量,80,81,82表示最后的ip地��第四位
设置IP的脚本
E:\VMS\set_ip.bat