2 # esxlist - list active domains of an ESX host and print some info.
3 # also demonstrates how to use the libvirt.openAuth() method
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
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] + ": ")
53 def print_section(title
):
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:
68 value
= res
[0].content
70 print_entry(key
, value
)
75 if len(sys
.argv
) != 2:
80 hostname
= sys
.argv
[1]
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)
103 print("Failed to open connection to %s" % hostname
)
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)
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/*")
136 ctx
.setContextNode(d
)
139 print("------------------------------------------------------------")
143 print_entry("Device", d
.name
)
145 type = print_xml("Type:", ctx
, "@type")
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")