VMware {code} Community
dbiswas91
Contributor
Contributor
Jump to solution

Getting error while listing all vms under ESXi 7.0 using python vsphere sdk 8.0

 

 

 

 

 

import requests
import urllib3
import logging

logging.basicConfig(filename='app.log', level=logging.DEBUG ,filemode='w', format='%(name)s - %(levelname)s - %(message)s')
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# ESxi 7.0
vsphere_client = create_vsphere_client(server='<ESXi 7.0 server ip>', username='root', password='x,x,x,x', session=session)
ab=vsphere_client.vcenter.VM.list()
print(ab)

 

# python3 list_vm.py

Traceback (most recent call last):
File "/root/vsphere-automation-sdk-python/dk_try.py", line 18, in <module>
vsphere_client = create_vsphere_client(server='xxxxx', username='root', password='xxxxxx', session=session)
File "/usr/local/lib/python3.9/site-packages/vmware/vapi/vsphere/client.py", line 173, in create_vsphere_client
return VsphereClient(session=session, server=server, username=username,
File "/usr/local/lib/python3.9/site-packages/vmware/vapi/vsphere/client.py", line 116, in __init__
session_id = session_svc.create()
File "/usr/local/lib/python3.9/site-packages/com/vmware/cis_client.py", line 201, in create
return self._invoke('create', None)
File "/usr/local/lib/python3.9/site-packages/vmware/vapi/bindings/stub.py", line 345, in _invoke
return self._api_interface.native_invoke(ctx, _method_name, kwargs)
File "/usr/local/lib/python3.9/site-packages/vmware/vapi/bindings/stub.py", line 295, in native_invoke
raise TypeConverter.convert_to_python(method_result.error, # pylint: disable=E0702
com.vmware.vapi.std.errors_client.OperationNotFound: {messages : [LocalizableMessage(id='vapi.provider.interface.unknown', default_message='Unknown interface: com.vmware.cis.session', args=['com.vmware.cis.session'], params=None, localized=None)], data : None, error_type : None}

 

similar way perl sdk works on single ESXi 6.5, 6.7 to list down vm on ESXi without any vcenter or vsphere server attached to ESXi

but in python it is not working. any one have any idea.

 

Reply
0 Kudos
1 Solution

Accepted Solutions
doskiran
Enthusiast
Enthusiast
Jump to solution

vSphere Automation SDK works only with vCenter sessions.

Because there are no public APIs for ESXi VMODL2.

To achieve your task use pyVmomi.
> pyVmomi(SOAP based) allows you to manage VMware ESXi and vCenter using Python.

pyVmomi: https://github.com/vmware/pyvmomi

Note:
Mentioned perl SDK is also a SOAP-based( vSphere Web Services API) integration point for the vSphere Management SDK.

Sample program to list all VMs under ESXi/vCenter:

from pyVim.connect import SmartConnect
import ssl

s = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
s.verify_mode = ssl.CERT_NONE

si = SmartConnect(
    host="<vCenter or ESXi server>",
    user="root",
    pwd="<passw0rd>",
    port=443,
    sslContext=s,
)

content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
    if hasattr(child, "vmFolder"):
        datacenter = child
        vmFolder = datacenter.vmFolder
        vmList = vmFolder.childEntity
        for vm in vmList:
            summary = vm.summary
            print("VM Name: ", summary.config.name)

 

 

 

View solution in original post

Reply
0 Kudos
1 Reply
doskiran
Enthusiast
Enthusiast
Jump to solution

vSphere Automation SDK works only with vCenter sessions.

Because there are no public APIs for ESXi VMODL2.

To achieve your task use pyVmomi.
> pyVmomi(SOAP based) allows you to manage VMware ESXi and vCenter using Python.

pyVmomi: https://github.com/vmware/pyvmomi

Note:
Mentioned perl SDK is also a SOAP-based( vSphere Web Services API) integration point for the vSphere Management SDK.

Sample program to list all VMs under ESXi/vCenter:

from pyVim.connect import SmartConnect
import ssl

s = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
s.verify_mode = ssl.CERT_NONE

si = SmartConnect(
    host="<vCenter or ESXi server>",
    user="root",
    pwd="<passw0rd>",
    port=443,
    sslContext=s,
)

content = si.RetrieveContent()
for child in content.rootFolder.childEntity:
    if hasattr(child, "vmFolder"):
        datacenter = child
        vmFolder = datacenter.vmFolder
        vmList = vmFolder.childEntity
        for vm in vmList:
            summary = vm.summary
            print("VM Name: ", summary.config.name)

 

 

 

Reply
0 Kudos