Product SiteDocumentation Site

7.3.2. Obtaining a virInterface instance for an Interface

Many operations require that you have an instance of virInterface, but you may only have the name or MAC address of the interface. You can use interfaceLookupByName and interfaceLookupByMACString to get the virInterface instance in these cases.

Example 7.7. Fetching the virInterface instance for a given interface name

Note: error handling omitted for clarity
# Example-7.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)

iface = conn.interfaceLookupByName('eth0')

print("The interface name is: "+iface.name())

conn.close()
exit(0)

Example 7.8. Fetching the virInterface instance for a given interface MAC Address

Note: error handling omitted for clarity
# Example-8.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)

iface = conn.interfaceLookupByMACString('00:01:02:03:04:05')

print("The interface name is: "+iface.name())

conn.close()
exit(0)