Skip to main content

A package that provides functions for interacting with an ESXi server and manipulating VMs/OVFs.

Project description

©2025 The MITRE Corporation. ALL RIGHTS RESERVED.

The author's affiliation with The MITRE Corporation is provided for identification purposes only, and is not intended to convey or imply MITRE's concurrence with, or support for, the positions, opinions, or viewpoints expressed by the author.'©2025 The MITRE Corporation. ALL RIGHTS RESERVED. NOTICE

This software was produced for the U. S. Government under Basic Contract No. W56KGU-18-D-0004, and is subject to the Rights in Noncommercial Computer Software and Noncommercial Computer Software Documentation Clause 252.227-7014 (FEB 2014)

Documentation

Ensure Sphinx is installed with pip install sphinx and then run make in the docs/ directory.

Examples

Some basic examples are included here for common functionality. For more detailed information, see the generated documentation.

Initialize a client

import esxi_utils
client = esxi_utils.ESXiClient("<ip>", "<username>", "<password>")

Note: The remaining examples will assume an already-initialized client.

Get Virtual Machine Information

# Print all VM names
print("All VMs: " + str(client.vms.names))

# Check if a VM exists
if "MyVirtualMachine" not in client.vms:  # Or more explicitly: if not client.vms.exists("MyVirtualMachine"):
   raise Exception("VM does not exist")

# Get a VM object
vm = client.vms["MyVirtualMachine"]       # Or more explicitly: vm = client.vms.get("MyVirtualMachine")

# Print this VMs information
print("Name: " + vm.name)
print("ID: " + str(vm.id))
print("UUID: " + vm.uuid)
print("Name of datastore containing this VM: " + vm.datastore.name)
print("Is powered on: " + str(vm.powered_on))
print("Number of VCPUs: " + str(vm.vcpus))
print("MB of memory: " + str(vm.memory))
print("VM OS Type: " + vm.guestid)

for disk in vm.disks:
   print("Disk size (KB): " + str(disk.size))

for nic in vm.nics:
   print("Network: " + nic.network + " (IP: " + nic.ip + ")")

for cdrom in vm.cdroms:
   print("CD-ROM: " + str(cdrom.file))

for snapshot in vm.snapshots:
   print("Snapshot: name=" + snapshot.name + ", description=" + snapshot.description + ", created=" + str(snapshot.createtime))

Create a new Virtual Machine and Add Hardware

vm = client.vms.create(
   name="MyNewVirtualMachine",
   datastore="MyDatastore",
   vcpus=1,
   memory="2GB",
   guestid="rhel7_64Guest"
)

# Add a disk
vm.disks.add("10GB")

# Add a NIC
vm.nics.add("Test-Network")

# Add a CD
cdrom_file = client.datastores["MyDatastore"].filepath("my-file.iso")	# Or: client.datastores["MyDatastore"].root / "my-file.iso"
cdrom = vm.cdroms.add(cdrom_file)
cdrom.start_connected = True

# Add a Floppy
floppy_file = client.datastores["MyDatastore"].filepath("my-floppy.img")
floppy = vm.floppies.add(floppy_file)
floppy.start_connected = True

Modify an existing Virtual Machine

vm = client.vms["MyVirtualMachine"]

vm.power_off()                  # Power off the virtual machine
vm.memory = "8GB"               # Set memory to 8GB
vm.vcpus = 4                    # Set number of VCPUs to 4
vm.guestid = "rhel7_64Guest"    # Set ESXi OS Type

# Modify a NIC
nic = vm.nics["External"]               # Get the NIC for network "External"; Or more explicitly: nic = vm.nics.get("External")
nic.network = "Management-Network"      # Change the NIC's network to "Management-Network"
nic.connected = True                    # Connect the NIC

# Remove a NIC
vm.nics["Other-Network"].remove()

# Resize the first disk
vm.disks[0].size = "32GB"

# Remove the second disk
vm.disks[1].remove()

# Change the CD-ROM file
vm.cdroms[0].file = client.datastores["MyDatastore"].filepath("installer.iso")

# Change the floppy file
vm.floppies[0].file = client.datastores["MyDatastore"].filepath("kickstart.img")

Work with Virtual Machine Snapshots

vm = client.vms["MyVirtualMachine"]

# Create a new snapshot
vm.snapshots.create("MyNewSnapshot", description="This is a new snapshot")

# Remove the current snapshot
vm.snapshots.current.remove(remove_children=True)

# Revert to an old snapshot
vm.snapshots["MyOldSnapshot"].revert()

Upload a Virtual Machine from OVF or OVA

vm = client.vms.upload(
   file="./path/to/file.ovf",
   datastore="MyDatastore",
   name="MyNewVirtualMachine",
   network_mappings={ "Original-Network": "New-Network" }
)

vm.power_on()

Export a Virtual Machine to OVF or OVA

client.vms["MyVirtualMachine"].export(
   path="./path/to/export/folder",
   format="ova",
   hash_type="sha256",
   include_image_files=False,
   include_nvram=True
)

Clone a Virtual Machine

vm = client.vms["MyVirtualMachine"].clone("MyNewVirtualMachine")
vm.power_on()

Print Networking Information

print("Host VSwitches:")
for vswitch in client.vswitches:
	print("\tName:" + vswitch.name)
	print("\t\tMTU: " + str(vswitch.mtu))
	print("\t\tPorts Available: " + str(vswitch.numports_available) + " / " + str(vswitch.numports))
	print("\t\tPort groups:")
	for portgroup in vswitch.portgroups:
		print("\t\t\t- " + portgroup.name)
print()

print("Host Port Groups:")
for portgroup in client.portgroups:
	print("\tName: " + portgroup.name)
	print("\t\tVLAN: " + str(portgroup.vlan))
	print("\t\tActive Clients: " + str(portgroup.active_clients))
	print("\t\tVSwitch: " + portgroup.vswitch.name)
	print("\t\tConnected VMs:")
	for vm in portgroup.vms:
		print("\t\t\t- " + vm.name)
print()

print("Host Physical NICs:")
for pnic in client.physicalnics:
	print("\tName: " + pnic.name)
	print("\t\tUp: " + str(pnic.up))
	print("\t\tLink Speed (MB): " + str(pnic.linkspeed))
	print("\t\tFull Duplex: " + str(pnic.fullduplex))
print()

print("Host VMKernel NICs:")
for vnic in client.vmkernelnics:
	print("\tName: " + vnic.name)
	print("\t\tPort Group: " + str(vnic.portgroup))
	print("\t\tIP: " + vnic.ip)
	print("\t\tSubnet: " + vnic.subnetmask)
	print("\t\tMTU: " + str(vnic.mtu))

Modify Networking

# Check if a virtual switch exists
exists = "MyVirtualSwitch" in client.vswitches
print("VSwitch exists: " + str(exists))

# Check if a port groups exists
exists = "MyPortGroup" in client.portgroups
print("PortGroup exists: " + str(exists))

# Add new virtual switch and port groups
vswitch = client.vswitches.add("TestVSwitch")
vswitch.add("TestPortGroup", vlan=70)
vswitch.add("TestPortGroup2", vlan=80)

# Remove a port group from a virtual switch
client.vswitches["TestVSwitch"].portgroups[0].remove()

# Remove a virtual switch
client.vswitches["TestVSwitch"].remove()

# Add a port group to an existing virtual switch
client.vswitches["AVirtualSwitch"].add("APortGroup", vlan=0)
client.vswitches["AVirtualSwitch"].add("APortGroup2", vlan=4095)

Development

References

Please refer to Broadcom's website for the latest documentation.

(This documentation no longer exists): Vsphere documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

esxi_utils-4.0.2.tar.gz (124.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

esxi_utils-4.0.2-py3-none-any.whl (154.2 kB view details)

Uploaded Python 3

File details

Details for the file esxi_utils-4.0.2.tar.gz.

File metadata

  • Download URL: esxi_utils-4.0.2.tar.gz
  • Upload date:
  • Size: 124.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for esxi_utils-4.0.2.tar.gz
Algorithm Hash digest
SHA256 c2396bb95da3ddef8fb71fbf96404863814ff9105bcf1314eca3228b7bb291e7
MD5 de3b9735ce9a393ef8d16b8ed6796fb3
BLAKE2b-256 a71fc3bdb42e15f45a5a6027c8feac938d6027a4668c6be4b3542aa3a28cde96

See more details on using hashes here.

Provenance

The following attestation bundles were made for esxi_utils-4.0.2.tar.gz:

Publisher: workflow.yml on mitre/Python-ESXi-Utilities

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file esxi_utils-4.0.2-py3-none-any.whl.

File metadata

  • Download URL: esxi_utils-4.0.2-py3-none-any.whl
  • Upload date:
  • Size: 154.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for esxi_utils-4.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2e78ae5430bc0db4d65579de64d3d1a87ad6e145c3802e343b1b46b10c9efc59
MD5 e301f2b7d4b4df8590aa2a80801834fe
BLAKE2b-256 db762d1f7f1530e59fc10a47e5dd2f3f8ce3e7ac302dd9c18679f5dee6955944

See more details on using hashes here.

Provenance

The following attestation bundles were made for esxi_utils-4.0.2-py3-none-any.whl:

Publisher: workflow.yml on mitre/Python-ESXi-Utilities

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page