Product SiteDocumentation Site

3.4. Host information

There are various Python virConnection methods that can be used to get information about the virtualization host, including the hostname, maximum support guest CPUs, etc.

3.4.1. getHostname

The getHostname method can be used to obtain the hostname of the virtualization host as returned by gethostname(). It invoked via a the virConnectioninstance and, if successful, returns a string containing the hostname possibly expanded to a fully-qualified domain name. If an error occurred, NULL will be returned instead. The following code demonstrates the use of getHostname:

Example 3.11. Using getHostname

# Example-9.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)

host = conn.getHostname()
print('Hostname:'+host)

conn.close()
exit(0)