How about to list all vm from vCenter,not only one Esxi
Closed this issue · 9 comments
how can I list all vm from all Esxi
not from vcenter
Hi @zsmlinux, ezmomi interfaces with vSphere, so it is not aware of anything outside of vcenter/vsphere. Sorry I couldn't help here.
Hi @zsmlinux can you explain in more detail what you're trying to do? I may not have understood the initial question.
I can't speak for @zsmlinux but I'm wondering if this is about linked vCenter servers. I'm currently trying to figure out how to leverage this in an environment with multiple vCenter servers that are all linked, as I don't always know the specific vCenter server that it runs on. I know PowerCLI can do this and there is a Perl script that can get all of the linked servers (via vSphere Perl API). If I'm wrong about what the original request was, then I can open a new issue/request about this specific item.
Perl script reference: http://www.virtuin.com/2012/12/querying-vcenter-linked-mode-with.html
If its possible via the perl vSphere API, I would bet its possible via the python API (pyvmomi). I'll try and investigate soon, but feel free to do a little digging too. Hopefully its as simple as specifying a list of servers to connect to.
Took some tinkering, but I cam up with something. This will connect to the specified vCenter server and then return a list of linked vCenter servers.
from pyVmomi.VmomiSupport import nsMap, versionIdMap, versionMap, IsChildVersion
from pyVmomi.VmomiSupport import GetServiceVersions, GetVersionNamespace
from pyVim import connect
import requests
from xml.etree.ElementTree import XML, fromstring, tostring
server='vsphere.server.com'
protocol='https'
port=443
path='sdk'
url = "%s://%s:%s/%s/webService" % (protocol, server, port, path)
baseVersion = 'vim25'
preferredApiVersions = GetServiceVersions(baseVersion)
supportedVersion = connect.__FindSupportedVersion(protocol, server, port, path, preferredApiVersions, None)
versionNS = GetVersionNamespace(supportedVersion)
versionId = '"urn:{0}"'.format(versionNS)
headers = {'content-type': 'text/xml; charset=utf-8', 'Accept': 'text/xml,multipart/*,application/soap', 'SOAPAction': versionId}
body="""<?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><QueryServiceEndpointList><_this type="ServiceDirectory" xsi:type="ManagedObjectReference">ServiceDirectory</_this></QueryServiceEndpointList></soap:Body></soap:Envelope>"""
response=requests.post(url,data=body,headers=headers, verify=False)
soapResponse = fromstring(response.content)
vCenterList=[]
returnvalList = soapResponse.findall('.//{urn:' + baseVersion + '}returnval')
for retVal in returnvalList:
node = retVal.find('{urn:' + baseVersion + '}protocol')
if node is not None and node.text == 'vimApi':
hostNode = retVal.find('{urn:' + baseVersion + '}instanceName')
if hostNode is not None:
vCenterList.append(hostNode.text)
### do something with vCenterList
All of this may not be required or done in the most efficient method, but I'm still getting my feet wet with python.
@JStinebaugh I use your nice script to test against two linked vCenter Servers 6.0. However it returns only 1 vCenter. When pointing the script to PSC IP address, it reports error '/sdk/webService
description The requested resource is not available'
Fortunately based on your script, I worked out a python script to list linked mode vCenter Servers 6.0 or other Service Endpoints from PSC https://gist.github.com/jessehu/2e28b2d8ab454e67712cd943a8500512