Created
December 10, 2013 13:59
-
-
Save alecnunn/7890963 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = "Alec Nunn" | |
| from pysphere import VIServer, MORTypes | |
| #-------------------------------------------------------- | |
| # CONFIGURATION | |
| #-------------------------------------------------------- | |
| # IP OF SERVER | |
| hostIP = "" | |
| # USERNAME | |
| username = "" | |
| # PASSWORD | |
| password = "" | |
| # LOG FILE NAME | |
| logFile = "debug.log" | |
| # TO ENABLE LOGGING CHANGE TO 'TRUE' | |
| logging = False | |
| # SEPARATOR | |
| separator = "_"*80 | |
| #-------------------------------------------------------- | |
| def getVMStatus(vmPath): | |
| VM = vmPath | |
| print "\tPowered On".ljust(25), VM.is_powered_on() | |
| print "\tPowered Off".ljust(25), VM.is_powered_off() | |
| print "\tSuspended".ljust(25), VM.is_suspended() | |
| print "\tPowering On".ljust(25), VM.is_powering_on() | |
| print "\tPowering Off".ljust(25), VM.is_powering_off() | |
| print "\tSuspending".ljust(25), VM.is_suspending() | |
| print "\tResetting".ljust(25), VM.is_resetting() | |
| print "\tBlocked On Message".ljust(25), VM.is_blocked_on_msg() | |
| print "\tReverting To Snapshot".ljust(25), VM.is_reverting() | |
| def getBasicStatus(vmPath): | |
| VM = vmPath | |
| try: | |
| print "\t Basic Status:".ljust(25), VM.get_status(basic_status=True) | |
| except: | |
| pass | |
| def getProperties(vmPath): | |
| VM = vmPath | |
| try: | |
| print "\tName:".ljust(25), VM.get_property("name", from_cache=False) | |
| print "\tPath:".ljust(25), VM.get_property("path", from_cache=False) | |
| print "\tIP Address:".ljust(25), VM.get_property("ip_address", from_cache=False) | |
| print "\tMAC Address:".ljust(25), VM.get_property("mac_address", from_cache=False) | |
| print "\tHostname:".ljust(25), VM.get_property("hostname", from_cache=False) | |
| print "\tNetwork:".ljust(25), VM.get_property("net", from_cache=False) | |
| print "\tGuest ID:".ljust(25), VM.get_property("guest_id", from_cache=False) | |
| print "\tGuest Full Name:".ljust(25), VM.get_property("guest_full_name", from_cache=False) | |
| print "\tSnapshots:".ljust(25), VM.get_snapshots() | |
| print "\tResource Pool:".ljust(25), VM.get_resource_pool_name() | |
| except: | |
| pass | |
| def conn(ip, user, password, dbg=False): | |
| server = VIServer() | |
| if dbg: | |
| server.connect(ip, user, password, trace_file=logFile) | |
| else: | |
| server.connect(ip, user, password) | |
| return server | |
| def getVMs(server): | |
| return server.get_registered_vms() | |
| def getVMName(server, vmpath): | |
| VM = server.get_vm_by_path(vmpath) | |
| print " VM Name:".ljust(25), VM.get_property("name", from_cache=False) | |
| def getVMPath(server, vmpath): | |
| VM = server.get_vm_by_path(vmpath) | |
| print "\tVM Path:".ljust(25), VM.get_property("path", from_cache=False) | |
| #-------------------------------------------------------- | |
| # TEST THE SCRIPT | |
| #-------------------------------------------------------- | |
| serv = conn(hostIP, username, password, logging) | |
| vmlist = serv.get_registered_vms(status="poweredOn") | |
| # print "\n".join(vmlist) | |
| print separator | |
| for vmpath in vmlist: | |
| VM = serv.get_vm_by_path(vmpath) | |
| getProperties(VM) | |
| getVMStatus(VM) | |
| getVMName(serv, vmpath) | |
| getVMPath(serv, vmpath) | |
| getBasicStatus(VM) | |
| print separator | |
| serv.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment