Add missing impl of virNetworkListAllPorts
[libvirt-python/ericb.git] / examples / esxlist.py
blob0d47b00fefe230a7171bc752f03ef1901b545d07
1 #!/usr/bin/env python
2 # esxlist - list active domains of an ESX host and print some info.
3 # also demonstrates how to use the libvirt.openAuth() method
5 import libvirt
6 import sys
7 import os
8 import libxml2
9 import getpass
12 def usage():
13 print("Usage: %s HOSTNAME" % sys.argv[0])
14 print(" List active domains of HOSTNAME and print some info")
17 # This is the callback method passed to libvirt.openAuth() (see below).
19 # The credentials argument is a list of credentials that libvirt (actually
20 # the ESX driver) would like to request. An element of this list is itself a
21 # list containing 5 items (4 inputs, 1 output):
22 # - the credential type, e.g. libvirt.VIR_CRED_AUTHNAME
23 # - a prompt to be displayed to the user
24 # - a challenge, the ESX driver sets this to the hostname to allow automatic
25 # distinction between requests for ESX and vCenter credentials
26 # - a default result for the request
27 # - a place to store the actual result for the request
29 # The user_data argument is the user data item of the auth argument (see below)
30 # passed to libvirt.openAuth().
31 def request_credentials(credentials, user_data):
32 for credential in credentials:
33 if credential[0] == libvirt.VIR_CRED_AUTHNAME:
34 # prompt the user to input a authname. display the provided message
35 credential[4] = raw_input(credential[1] + ": ")
37 # if the user just hits enter raw_input() returns an empty string.
38 # in this case return the default result through the last item of
39 # the list
40 if len(credential[4]) == 0:
41 credential[4] = credential[3]
42 elif credential[0] == libvirt.VIR_CRED_NOECHOPROMPT:
43 # use the getpass module to prompt the user to input a password.
44 # display the provided message and return the result through the
45 # last item of the list
46 credential[4] = getpass.getpass(credential[1] + ": ")
47 else:
48 return -1
50 return 0
53 def print_section(title):
54 print("\n%s" % title)
55 print("=" * 60)
58 def print_entry(key, value):
59 print("%-10s %-10s" % (key, value))
62 def print_xml(key, ctx, path):
63 res = ctx.xpathEval(path)
65 if res is None or len(res) == 0:
66 value = "Unknown"
67 else:
68 value = res[0].content
70 print_entry(key, value)
72 return value
75 if len(sys.argv) != 2:
76 usage()
77 sys.exit(2)
80 hostname = sys.argv[1]
82 # Connect to libvirt
83 uri = "esx://%s/?no_verify=1" % hostname
85 # The auth argument is a list that contains 3 items:
86 # - a list of supported credential types
87 # - a callable that takes 2 arguments
88 # - user data that will be passed to the callable as second argument
90 # In this example the supported credential types are VIR_CRED_AUTHNAME and
91 # VIR_CRED_NOECHOPROMPT, the callable is the unbound method request_credentials
92 # (see above) and the user data is None.
94 # libvirt (actually the ESX driver) will call the callable to request
95 # credentials in order to log into the ESX host. The callable would also be
96 # called if the connection URI would reference a vCenter to request credentials
97 # in order to log into the vCenter
98 auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_NOECHOPROMPT],
99 request_credentials, None]
100 conn = libvirt.openAuth(uri, auth, 0)
102 if conn is None:
103 print("Failed to open connection to %s" % hostname)
104 sys.exit(1)
106 state_names = { libvirt.VIR_DOMAIN_RUNNING : "running",
107 libvirt.VIR_DOMAIN_BLOCKED : "idle",
108 libvirt.VIR_DOMAIN_PAUSED : "paused",
109 libvirt.VIR_DOMAIN_SHUTDOWN : "in shutdown",
110 libvirt.VIR_DOMAIN_SHUTOFF : "shut off",
111 libvirt.VIR_DOMAIN_CRASHED : "crashed",
112 libvirt.VIR_DOMAIN_NOSTATE : "no state" }
114 for id in conn.listDomainsID():
115 domain = conn.lookupByID(id)
116 info = domain.info()
118 print_section("Domain " + domain.name())
119 print_entry("ID:", id)
120 print_entry("UUID:", domain.UUIDString())
121 print_entry("State:", state_names[info[0]])
122 print_entry("MaxMem:", info[1])
123 print_entry("UsedMem:", info[2])
124 print_entry("VCPUs:", info[3])
126 # Read some info from the XML desc
127 print_section("Devices of " + domain.name())
129 xmldesc = domain.XMLDesc(0)
130 doc = libxml2.parseDoc(xmldesc)
131 ctx = doc.xpathNewContext()
132 devs = ctx.xpathEval("/domain/devices/*")
133 first = True
135 for d in devs:
136 ctx.setContextNode(d)
138 if not first:
139 print("------------------------------------------------------------")
140 else:
141 first = False
143 print_entry("Device", d.name)
145 type = print_xml("Type:", ctx, "@type")
147 if type == "file":
148 print_xml("Source:", ctx, "source/@file")
149 print_xml("Target:", ctx, "target/@dev")
150 elif type == "block":
151 print_xml("Source:", ctx, "source/@dev")
152 print_xml("Target:", ctx, "target/@dev")
153 elif type == "bridge":
154 print_xml("Source:", ctx, "source/@bridge")
155 print_xml("MAC Addr:", ctx, "mac/@address")