python 自动创建Hype-V虚拟机脚本
安装模块
pip install pywinrm
脚本如下
#!/usr/bin/env python3
# coding=utf-8
# author:LJX
# describe:一键创建hype-v虚拟机
# createdate:2021.5.26
import winrm
class VM(object):
'''
# 1、查看winrm服务状态。默认没有启动 winrm enumerate winrm/config/listener
# 2、启动服务器(网络类型改为专有网络) winrm quickconfig
# 3、开启防火墙 netsh advfirewall firewall set rule group="Windows 远程管理" new enable=yes
# 4、启动 winrm quickconfig
# 5、查看 winrm enumerate winrm/config/listener
# 5、为winrm service 配置auth winrm set winrm/config/service/auth @{Basic="true"}
# 7、为winrm service 配置加密方式为允许非加密 winrm set winrm/config/service @{AllowUnencrypted="true"}
'''
def __init__(self):
self.host = "xxxx"
self.port = "xxxx"
self.user = "xxxxx"
self.passwd = 'xxxx'
self.linuxfile = r"xxxxxxx"
self.winfile = r"xxxxxx"
self.linuxPath = '''D:\{0}'''.format(self.linuxfile)
self.winPath = '''D:\{0}'''.format(self.winfile)
self.network = "wan"
self.wintest = winrm.Session('http://{0}:{1}/wsman'.format(self.host, self.port), auth=(self.user, self.passwd))
def exec_ps(self, commond,msg):
ret = self.wintest.run_ps(commond)
if ret.status_code == 0:
print(msg + "成功")
elif ret.status_code == 1:
print(msg + "失败")
print(str(ret.std_out, "utf-8"))
def add_vm(self, params):
hostname = params["ip"] +"-"+ params["projectName"]
path = "{0}:\{1}".format(params["diskPath"], hostname)
self.exec_ps(r"new-item -path {0} -type directory".format(path),"创建虚拟机目录")
# 2、
if params["sysType"] == "linux":
self.exec_ps(r'''copy-item {0} {1}'''.format(self.linuxPath, path),"复制系统")
self.exec_ps(
r"New-VM -VHDPath {0}\{1} -MemoryStartupBytes {2}GB -Name {3} -SwitchName {4}".format(path,
self.linuxfile,
params["mem"],
hostname,
self.network),
"创建虚拟机"
)
elif params["sysType"] == "windows":
self.exec_ps(r'''copy-item {0} {1}'''.format(self.winPath, path),"复制系统")
self.exec_ps(
r"New-VM -VHDPath {0}\{1} -MemoryStartupBytes {2}GB -Name {3} -SwitchName {4}".format(path,
self.winfile,
params["mem"],
hostname,
self.network),
"创建虚拟机"
)
self.exec_ps("Set-VMProcessor -VMName {0} -Count {1}".format(hostname, params["core"]),"设置核心数")
if params["otherDisk"]["enable"]:
otherDiskPath = "{0}\{1}".format(path,params["otherDisk"]["name"])
self.exec_ps("New-VHD -Path {0} -SizeBytes {1}GB".format(otherDiskPath,params["otherDisk"]["size"]),"创建磁盘")
self.exec_ps("Add-VMHardDiskDrive -VMName {0} -Path {1}".format(hostname,otherDiskPath),"虚拟机挂载磁盘")
if __name__ == "__main__":
vm_params = {
"projectName": "vmtest",
"ip": "xxxxxx",
"mem": 4,
"core": 4,
"disk": 100,
"diskPath": "F",
"sysType": "linux",
"otherDisk":{
"name": "disk1.vhdx",
"enable": True,
"size": 10,
}
}
vm = VM()
vm.add_vm(vm_params)
python 自动创建Hype-V虚拟机脚本的更多相关文章
- VMware 自动开多台虚拟机脚本
d:cd "D:\WinInstall\VMware\VMware Workstation"ECHO "start vm1"vmrun -T ws start ...
- 运维常用shell脚本一(系统指标巡检、自动创建用户、跳板机)
一.系统指标巡检脚本 #!/bin/bash menu(){ cat <<EOF +---------------------------------------------+ | 日常巡 ...
- 自动创建新序列号的Cookies脚本
已知一个网站在被访问的时候会读取电脑上存储的cookies 如果已经有cookie变量存在 则在存在的变量后按顺序增加新的序列 如电脑上有vst1变量的cookie了 那么新用户则自动创建为 vst2 ...
- 使用Vagrant创建多节点虚拟机集群
摘要: 在前一篇博客中,我介绍了使用Vagrant快速创建虚拟机,但是所创建的只是单个虚拟机.这篇博客将介绍使用Vagrant创建多节点虚拟机集群,可以作为Hadoop,Spark以及Storm等分布 ...
- python自动开发之(ajax)第二十天
1.Django请求的生命周期 路由系统 -> 试图函数(获取模板+数据=>渲染) -> 字符串返回给用户 2.路由系统 /index/ -> 函数或类.as_view() / ...
- python自动安装mysql5.7
python自动安装mysql5.7 python版本:python2.6 centos版本:centos6.9 mysql版本:mysql5.7.19 安装目录路径和数据目录路径都是固定,当然也可以 ...
- python自动安装mysql5.7【转】
#!/usr/bin/env python import os import sys import re base_dir = '/opt/software/mysql-5.7.17-linux-gl ...
- Python自动发布Image service的实现
使用Python自动发布地图服务已经在上一篇博客中讲到,使用Python创建.sd服务定义文件,实现脚本自动发布ArcGIS服务,下面是利用Python自动发布Image service的实现. -- ...
- 自动化工具 Python 调 Jmeter 执行参数化 jmx 脚本
烦透了jmeter输入如下鬼命令: Jmeter -n -t {tmpjmxfile} -l {csvfilename} -e -o {htmlreportpath} 尤其是{htmlreportpa ...
- 利用python自动生成verilog模块例化模板
一.前言 初入职场,一直忙着熟悉工作,就没什么时间更新博客.今天受“利奇马”的影响,只好宅在家中,写写技术文章.芯片设计规模日益庞大,编写脚本成了芯片开发人员必要的软技能.模块端口动不动就几十上百个, ...
随机推荐
- 【Springboot】项目启动后执行特定方法
Springboot项目启动后执行特定方法 Springboot给我们提供了两种"开机启动"方式:ApplicationRunner和CommandLineRunner. 这两种方 ...
- Django: AttributeError: 'str' object has no attribute 'decode'
Django安装Mysql驱动 pip install PyMySQL 在Django的工程同名子目录的__init__.py文件中添加如下语句 from pymysql import install ...
- Django: 获取头信息
如何获取请求头信息 使用如下函数request.META.get("HTTP_请求头函数"),需要注意的是,请求头变量需要全部大写. 其他注意事项如下所示: 如果headerkey ...
- 彻底搞懂Vue针对数组和双向绑定(MVVM)的处理方式
欢迎关注我的博客:https://github.com/wangweianger/myblog Vue内部实现了一组观察数组的变异方法,例如:push(),pop(),shift()等. Object ...
- 发布关于PostGIS对于USD格式的拓展
我们非常高兴的发布为了一年一度的SIGGRAPH 2023发布关于为PostGIS支持USD格式的新拓展. 新添加了3个函数 ST_AsUSDA(geom geometry, usd_root_nam ...
- 带你走进数仓大集群内幕丨详解关于作业hang及残留问题定位
本文分享自华为云社区<[带你走进DWS大集群内幕]大集群通信:作业hang.残留问题定位>,作者: 雨落天穹丶. 前言: 测试过程中,我们会遇到这样一种情况,我的作业都执行很久了,为啥还不 ...
- Apache-Log4j2-Rce漏洞复现
最近最热门的无非是最近爆出的超大boss-Apache log4j2组件的rce漏洞.安全圈俗称'过年',漏洞影响范围之广,危害之大堪比当年的永恒之蓝.由于最近爆出,危害程度目前还正在不断扩大中.超多 ...
- 论文日记五:QueryInst
导读 实例分割领域21年的SOTA,整个模型结构设计总述为以下: 1)设计了类似于Cascade Mask R-CNN的多阶段迭代优化的bbox和mask预测头: 2)基于query的实例分割方法,延 ...
- OpenLDAP 自助修改密码系统
一.创建docker-compose文件 root@xx:~# mkdir self-service-password root@xx:~# cd self-service-password root ...
- 每天一道面试题:Spring的Bean生命周期
Spring的Bean生命周期包括以下步骤: 1.实例化(Instantiation):当Spring容器接收到创建Bean的请求时,它会先实例化Bean对象.这个过程可以通过构造函数.工厂方法或者反 ...