【问题标题】:python list all vm with ip on vspherepython在vsphere上列出所有带有ip的vm
【发布时间】:2020-10-12 06:35:07
【问题描述】:

我正在尝试从我的 vsphere 服务器获取 vm 名称和 ip,目前我只有 vm 的名称。如何获取ip?

#! /usr/bin/env python
from pyVim.connect import SmartConnect, SmartConnectNoSSL, Disconnect
from pyVmomi import vim
import json

from pyVim.connect import SmartConnect
from pyVmomi import vim
import ssl

# Get all the Vms from vCenter server inventory and print its name
# Below is Python 2.7.x code, which can be easily converted to python 3.x version

s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode=ssl.CERT_NONE
si= SmartConnectNoSSL(host="192.168.1.2", user="titi@gege.com", pwd="my_pass")
content=si.content

# Method that populates objects of type vimtype
def get_all_objs(content, vimtype):
        obj = {}
        container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
        for managed_object_ref in container.view:
                obj.update({managed_object_ref: managed_object_ref.name})
        return obj

#Calling above method
getAllVms=get_all_objs(content, [vim.VirtualMachine])

#Iterating each vm object and printing its name
for vm in getAllVms:
        print (vm.name)

【问题讨论】:

  • 语言改进

标签: python-3.x vmware vsphere pyvmomi


【解决方案1】:

每个 VM 的 IP 地址信息位于 GuestInfo 对象中,该对象位于:vm.guestinfo.ipaddress

请注意,返回的 IP 地址来自 VMware Tools,因此请确保它们已安装并正在运行。

【讨论】:

  • 我是怎么做到的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 2014-10-09
  • 2018-05-16
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
相关资源
最近更新 更多