zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    静态切割窗体+关联对话框
    POJ 2236 Wireless Network(并查集)
    怎样学习(3):迭代学习,精益求精
    【菜鸟也疯狂UML系列】——概述
    OpenGL 资源汇编
    vue之mapMutaions的使用 && vuex中 action 用法示例 && api.js的使用
    内置组件 && vue中强大的缓存机制之keep-alive
    vue生命周期及使用 && 单文件组件下的生命周期
    vue中遇到的坑 --- 变化检测问题(数组相关)
    如何去除vue项目中的 # --- History模式
  • 原文地址:https://www.cnblogs.com/lanheader/p/15349257.html
Copyright © 2011-2022 走看看