Skip to content

Instantly share code, notes, and snippets.

@apua
Created June 10, 2021 08:13
Show Gist options
  • Select an option

  • Save apua/aa63343fbc6bce6ea06f7f9c0bcdddc5 to your computer and use it in GitHub Desktop.

Select an option

Save apua/aa63343fbc6bce6ea06f7f9c0bcdddc5 to your computer and use it in GitHub Desktop.
List Physical NICs on ESXi (as `esxcli network list` does)
# :author: Apua
# :ref: /bin/esxcli.py 6.5.0
# :target: get results of :cmd:`esxcli network nic list` in Python
#
# Note:
#
# - This script must run on ESXi console.
# - :lib:`vmware` is built-in on ESXi.
# - :obj:`session` must live during communicating with ESXi;
# and be set to `None` in order to disconnect.
# - "mo" stands for "Managed Object"
from vmware.esxcli.Session import Session, SessionOptions
from pyVmomi import VmomiSupport, DynamicTypeManagerHelper
session = Session(SessionOptions({}))
type_importer = DynamicTypeManagerHelper.DynamicTypeImporter(session.stub, hostSystem=None)
type_importer.ImportTypes('vim.EsxCLI.network.nic')
mo_type = VmomiSupport.GetVmodlType('vim.EsxCLI.network.nic')
mo = next(mo for mo in type_importer.GetTypeManager().QueryMoInstances() if mo.moType == 'vim.EsxCLI.network.nic')
assert mo.id == 'ha-cli-handler-network-nic'
namespace = mo_type(mo.id, session.stub)
list_nics = getattr(namespace, 'list'.capitalize())
for r in list_nics():
print(r.Name, r.LinkStatus)
session = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment