Product SiteDocumentation Site

7.3. Retrieving Information About Interfaces

7.3.1. Enumerating Interfaces

Once you have a connection to a host you can determine the number of interfaces on the host with numOfInterfaces and numOfDefinedInterfaces method. A list of those interfaces' names can be obtained with listInterfaces method and listDefinedInterfaces method ("defined" interfaces are those that have been defined, but are currently inactive). The list methods return a Python list. All four functions return None if an error is encountered.

Example 7.5. Getting a list of active ("up") interfaces on a host

Note: error handling omitted for clarity
# Example-5.py
#!/usr/bin/env python3
import sys
import libvirt

conn = None
try:
    conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)
    exit(1)

ifaceNames = conn.listInterfaces()

print("Active host interfaces:")
for ifaceName in ifaceNames:
    print('  '+ifaceName)

conn.close()
exit(0)

Example 7.6. Getting a list of inactive ("down") interfaces on a host

# Example-6.py
#!/usr/bin/env python3
import sys
import libvirt

conn = None
try:
    conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
    print(repr(e), file=sys.stderr)
    exit(1)

ifaceNames = conn.listDefinedInterfaces()

print("Inactive host interfaces:")
for ifaceName in ifaceNames:
    print('  '+ifaceName)

conn.close()
exit(0)