💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
windows下批量操作VM虚拟机 需要安装gitbash工具,然后运行下面脚本 将脚本中的路径修改为自己的VM路径 修改IP的操作是由此脚本生成windows的bat脚本然后再运行. 查看帮助直接运行脚本即可,不接任何参数 ``` #!/bin/bash #Author:Alaska #Blog:null #Time:2018-06-08 20:15:26 #Name:auto_vm.sh #Version:V1.0 ### 模版机位置 PATH_TEM="/E/VMS/Centos7/template/template.vmx" ### linux格式的虚拟机存放目录 PATH_SERVICE="/E/VMS/Centos7" ### windwos格式的的虚拟机存放目录 PATH_SERVICE_CMD="E:\VMS\Centos7" ### vm命令工具的路径 PATH_VMRUN_CMD="E:\\SOFT\\VM" ### 脚本存放路径 PATH_VMS="E:\\VMS" ### 要创建虚拟的主机名 SERVER_NAME="redis" ### 要创建虚拟的服务名称 CLUSTER_NAME="redis-cluster" # 创建克隆链接的虚拟机 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" CLEAN sleep 2 done } case $1 in create) VM_create ;; start) VM_start ;; stop) VM_stop ;; shot) VM_snapshot ;; set_ip) VM_set_IP ;; *) echo "bash $0 {create|start|set_ip|stop|shot}" esac ``` # 更加完善的脚本,增加了快照恢复和删除快照功能 ``` $ cat auto_VM.sh #!/bin/bash #Author:Alaska #Blog:null #Time:2018-06-08 20:15:26 #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="redis" CLUSTER_NAME="redis-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 ```