- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在考虑将现有的 powercli 部署脚本移动到 python/pyvmomi,以获得多线程(它部署了大量虚拟机)。原始脚本大量使用 Invoke-VMScript
通过 VMware Tools 将 powershell 片段推送到每个 guest 。
pyvmomi 中的等效功能是什么?具体来说 - 通过工具(而不是访客网络)向访客发送 powershell 脚本,让它使用提供的凭据运行,然后收集输出?
我可以看到 processManager.StartProgramInGuest
,但这似乎是一个笨拙的 3 步过程(上传文件、运行文件、下载重定向结果) - 这就是 powercli 在后台执行的操作吗?
最佳答案
因此,为了提供一些结论,并且因为我无论如何都找不到完整的示例,这是我对此的第一次尝试。它是已使用 SmartConnect
连接到 vcenter 服务器并设置 self.si
的类的一部分。它实际上还没有做太多的错误检查。您可以选择是否要等待并获取输出,或者只是在启动命令后返回。 remote_cmd
最初来自 pyvmomi-community-samples
,因此目前这两种方法之间存在一些重复。
def invoke_vmscript(self, vm_username, vm_password, vm_name, script_content, wait_for_output=False):
script_content_crlf = script_content.replace('\n', '\r\n')
content = self.si.content
creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
vm = self.get_vm(vm_name)
logger.debug("Invoke-VMScript Started for %s", vm_name)
logger.debug("CREATING TEMP OUTPUT DIR")
file_manager = content.guestOperationsManager.fileManager
temp_dir = file_manager.CreateTemporaryDirectoryInGuest(vm, creds, "nodebldr_",
"_scripts")
try:
file_manager.MakeDirectoryInGuest(vm, creds, temp_dir, False)
except vim.fault.FileAlreadyExists:
pass
temp_script_file = file_manager.CreateTemporaryFileInGuest(vm, creds, "nodebldr_",
"_script.ps1",
temp_dir)
temp_output_file = file_manager.CreateTemporaryFileInGuest(vm, creds, "nodebldr_",
"_output.txt",
temp_dir)
logger.debug("SCRIPT FILE: " + temp_script_file)
logger.debug("OUTPUT FILE: " + temp_output_file)
file_attribute = vim.vm.guest.FileManager.FileAttributes()
url = file_manager.InitiateFileTransferToGuest(vm, creds, temp_script_file,
file_attribute,
len(script_content_crlf), True)
logger.debug("UPLOAD SCRIPT TO: " + url)
r = requests.put(url, data=script_content_crlf, verify=False)
if not r.status_code == 200:
logger.debug("Error while uploading file")
else:
logger.debug("Successfully uploaded file")
self.remote_cmd(vm_name, vm_username, vm_password, 'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe',
"-Noninteractive {0} > {1}".format(temp_script_file, temp_output_file), temp_dir,
wait_for_end=wait_for_output)
output = None
if wait_for_output:
dl_url = file_manager.InitiateFileTransferFromGuest(vm, creds,
temp_output_file)
logger.debug("DOWNLOAD OUTPUT FROM: " + dl_url.url)
r = requests.get(dl_url.url, verify=False)
output = r.text
logger.debug("Script Output was: %s", output)
logger.debug("DELETING temp files & directory")
file_manager.DeleteFileInGuest(vm, creds, temp_script_file)
file_manager.DeleteFileInGuest(vm, creds, temp_output_file)
file_manager.DeleteDirectoryInGuest(vm, creds, temp_dir, True)
logger.debug("Invoke-VMScript COMPLETE")
return output
def remote_cmd(self, vm_name, vm_username, vm_password, command, args, working_dir, wait_for_end=False, timeout=60):
creds = vim.vm.guest.NamePasswordAuthentication(username=vm_username, password=vm_password)
vm = self.get_vm(vm_name)
try:
cmdspec = vim.vm.guest.ProcessManager.ProgramSpec(arguments=args, programPath=command)
pid = self.si.content.guestOperationsManager.processManager.StartProgramInGuest(vm=vm, auth=creds,
spec=cmdspec)
logger.debug("Started process %d on %s", pid, vm_name)
except vmodl.MethodFault as error:
print("Caught vmodl fault : ", error.msg)
return -1
n = timeout
if wait_for_end:
while n > 0:
info = self.si.content.guestOperationsManager.processManager.ListProcessesInGuest(vm=vm, auth=creds,
pids=[pid])
if info[0].endTime is not None:
break
logger.debug("Process not yet completed. Will wait %d seconds", n)
sleep(1)
n = n - 1
logger.debug("Process completed with state: %s", info[0])
关于python - powercli 中的 invoke-vmscript 的 pyvmomi 等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928104/
我有很多 VM(用 2 台主机调度),我想列出所有这些。 要做到这一点,我使用这个: atexit.register(connect.Disconnect, self.si) content = se
我目前正在使用 python(使用 Pyvmomi)开发 VMware SDK,我在所有示例或示例中看到这一行: atexit.register(Disconnect, self.si) 有谁知道应该
我使用 pyvmomi 连接到 vsphere 实例。我可以列出虚拟机。但是,我正在寻找有关如何执行其他任务(例如复制虚拟机、创建新虚拟机)的文档。是否有此类任务的示例代码或相关的 API 文档? 最
我最近升级到了 OSX High Sierra,现在每当我尝试在 Mac 上的 Ansible 中使用 vmware_guest 时都会收到此消息: fatal: [localhost -> loca
我正在尝试在 vCenter Server 设备上运行 Pyvmomi 社区示例中的一些示例脚本: https://github.com/vmware/pyvmomi-community-sample
我正在尝试使用 pyvmomi 通过 python 脚本将 OVF 部署到 vSphere 5.5,因为出现错误 对象不支持该操作。我能深入了解此错误的是打印具有此输出的 ImportVApp.err
目前我使用 vim-cmd 在我的 VMware 中心执行多项操作。 我正在使用 SSH paramiko 模块连接并检索 vim-cmd 命令状态: vim-cmd vmsvc/getallvms
我在虚拟交换机上有多个虚拟端口组。当我执行 datacenters = si.RetrieveContent().rootFolder.childEntity for datacenter in da
我想了解pyvmomi中的vim。 我知道 vim 是这样导入的:from pyvmomi import vim 我试图找到在pyvmomi中定义vim的位置,但我还没有找到。 我尝试了以下步骤: >
我需要编写 python 脚本来自动配置在 ESX/ESXi 主机上运行的虚拟机的时间。 我不知道该使用哪个 API。 我能够找到 VMWare API 的 python 绑定(bind),即。 Py
有没有办法在 vmware 中获取虚拟机的 IPStack?特别是在 python 中使用 pyvmomi https://pubs.vmware.com/vi3/sdk/ReferenceGuide
我被要求编写一些 python 代码,将 VMWare ESXi 主机置于维护模式。我得到了一个虚拟中心的名称,test-vc,以及一个 ESXi 主机的主机名,test-esxi-host 和这个链
过去一两天我在这里和 ole Google 上做了很多研究,但似乎找不到任何使用 pyVmomi(或任何其他 vSphere 模块)将孤立虚拟机添加到 list 中的示例那件事)。 感谢 github
一般来说,我的目标是根据名称获取未安装 VMTools 的 VM 的 IP。 FindByIp 的文档说 The IP address for a virtual machine is the one
我正在尝试使用 Pyvmomi 创建一个新的虚拟机。我成功创建了带有 RAM 和 CPU 的虚拟机,但我找不到有关如何创建带有附加磁盘的虚拟机的文档。我希望创建一个具有 20GB 精简配置 HDD 的
有没有办法在 ESXi 主机上启用 SSH?我环顾四周,但一无所获。我正在编写一个脚本,需要能够在主机上启用和禁用 SSH。 最佳答案 是的,有多种方法可以在 ESXi 主机上启用 SSH。 使用 P
我一直在浏览官方 VMWare pyvmomi API 的文档和示例,但在我的一生中,我找不到一个明显的方法来将 VM 从 ESXi 服务器上删除。 任何人都可以提供一个例子或向我指出已经实现过这个的
我正在使用下面的代码将配置有 DistributedVirtualSwitch 的 NIC 添加到现有 VM(通过 pyVmomi): def __AddNIC(si, vmconf_dict, ne
我正在使用 pyvmomi 使用 ovf 模板将虚拟机部署到 vCenter。默认情况下,它采用 .ovf 文件中指定的虚拟机名称。我无法弄清楚在我的代码中(即在创建导入规范时或在 ImportVap
我正在考虑将现有的 powercli 部署脚本移动到 python/pyvmomi,以获得多线程(它部署了大量虚拟机)。原始脚本大量使用 Invoke-VMScript 通过 VMware Tools
我是一名优秀的程序员,十分优秀!