Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <p>def print_vm_info(virtual_machine, depth=1):<br />
- """<br />
- Print information for a particular virtual machine or recurse into a<br />
- folder with depth protection<br />
- """<br />
- maxdepth = 10</p>
- <p> # if this is a group it will have children. if it does, recurse into them<br />
- # and then return<br />
- if hasattr(virtual_machine, 'childEntity'):<br />
- if depth > maxdepth:<br />
- return<br />
- vmList = virtual_machine.childEntity<br />
- for c in vmList:<br />
- print_vm_info(c, depth + 1)<br />
- return</p>
- <p> summary = virtual_machine.summary<br />
- print "Name : ", summary.config.name<br />
- print "Path : ", summary.config.vmPathName<br />
- print "Guest : ", summary.config.guestFullName<br />
- print "Instance UUID : ", summary.config.instanceUuid<br />
- print "Bios UUID : ", summary.config.uuid<br />
- annotation = summary.config.annotation<br />
- if annotation:<br />
- print "Annotation : ", annotation<br />
- print "State : ", summary.runtime.powerState<br />
- if summary.guest is not None:<br />
- ip_address = summary.guest.ipAddress<br />
- tools_version = summary.guest.toolsStatus<br />
- if tools_version is not None:<br />
- print "VMware-tools: ", tools_version<br />
- else:<br />
- print "Vmware-tools: None"<br />
- if ip_address:<br />
- print "IP : ", ip_address<br />
- else:<br />
- print "IP : None"<br />
- if summary.runtime.question is not None:<br />
- print "Question : ", summary.runtime.question.text<br />
- print ""</p>
- <p><br />
- def main():<br />
- """<br />
- Simple command-line program for listing the virtual machines on a system.<br />
- """</p>
- <p> args = cli.get_args()</p>
- <p> try:<br />
- service_instance = connect.SmartConnect(host=args.host,<br />
- user=args.user,<br />
- pwd=args.password,<br />
- port=int(args.port))</p>
- <p> atexit.register(connect.Disconnect, service_instance)</p>
- <p> content = service_instance.RetrieveContent()<br />
- children = content.rootFolder.childEntity<br />
- for child in children:<br />
- if hasattr(child, 'vmFolder'):<br />
- datacenter = child<br />
- else:<br />
- # some other non-datacenter type object<br />
- continue</p>
- <p> vm_folder = datacenter.vmFolder<br />
- vm_list = vm_folder.childEntity<br />
- for virtual_machine in vm_list:<br />
- print_vm_info(virtual_machine, 10)</p>
- <p> except vmodl.MethodFault as error:<br />
- print "Caught vmodl fault : " + error.msg<br />
- return -1</p>
- <p> return 0</p>
- <p># Start program<br />
- if __name__ == "__main__":<br />
- main()</p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement