def vc_connect(): print('Connecting to vCenter.....', vc_name) s_instance = connect.SmartConnectNoSSL(host=vc_name, user=vc_user, pwd=pass_vcuser, port=443) root_content = s_instance.RetrieveContent() return root_content
def main(): """ Simple command-line program for listing the virtual machines on a system. """ args = get_args() try: if args.disable_ssl_verification: service_instance = connect.SmartConnectNoSSL(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) else: service_instance = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password, port=int(args.port)) atexit.register(connect.Disconnect, service_instance) content = service_instance.RetrieveContent() container = content.rootFolder # starting point to look into viewType = [vim.VirtualMachine] # object types to look for recursive = True # whether we should look into it recursively containerView = content.viewManager.CreateContainerView( container, viewType, recursive) children = containerView.view if args.find is not None: pat = re.compile(args.find, re.IGNORECASE) for child in children: if args.find is None: print(child.summary.guest.ipAddress) print(child.summary.config.instanceUuid) print(child.summary.runtime.powerState) if len(child.guest.net[0].ipAddress) > 1: print(child.guest.net[0].ipAddress[1]) else: print(child.guest.net[0].ipAddress[0]) else: if pat.search(child.summary.config.name) is not None: # print_vm_info(child) print(child.summary.guest.ipAddress) print(child.summary.config.instanceUuid) print(child.summary.runtime.powerState) if len(child.guest.net[0].ipAddress) > 1: print(child.guest.net[0].ipAddress[1]) else: print(child.guest.net[0].ipAddress[0]) # prn_obj(child.guest) except vmodl.MethodFault as error: print("Caught vmodl fault : " + error.msg) return -1 return 0
def main(): try: si = None try: print("Trying to connect to VCENTER SERVER . . .") si = connect.SmartConnectNoSSL('https', config['VC_IP'], 443, '*****@*****.**', config['VC_SSO_PWD']) except IOError as e: pass atexit.register(Disconnect, si) print("Connected to VCENTER SERVER !") content = si.content # Get all Clusters clusters = get_all_objs(content, [vim.ClusterComputeResource]) # For ESXi host hosts = get_all_objs(content, [vim.HostSystem]) # For datacenters dcs = get_all_objs(content, [vim.Datacenter]) # For datastores datastores = get_all_objs(content, [vim.Datastore]) # Iterating each datacenter object and printing its name for dc in dcs: print("Datacenter found:", dc.name) # Iterating each host object and printing its name for cluster in clusters: print("Cluster found :", cluster.name) if cluster.name == config["VC_CLUSTER"]: print("Found what we were looking for", config["VC_CLUSTER"]) return cluster else: print("Could NOT find what we were looking for", config["VC_CLUSTER"]) # Iterating each cluster object and printing its name for host in hosts: print("Host found :", host.name) # Iterating each cluster object and printing its name for datastore in datastores: print("Datastore found:", datastore.name) # Get all ESXi hosts from the VC object_view = content.viewManager.CreateContainerView( content.rootFolder, [vim.HostSystem], True) host_list = object_view.view print(host_list) object_view.Destroy() for host in host_list: #if host.name == args.esx_host: esx = host print("ESX Name", esx.name) except vmodl.MethodFault as e: print("Caught vmodl fault: %s" % e.msg) return 1 except Exception as e: print("Caught exception: %s" % str(e)) return 1
vm_col += 1 for item in hw_header: worksheet_hosts.write(hw_row, hw_col, item, bold) hw_col += 1 # Set row to start after headers vm_row = 1 hw_row = 1 for host in hosts: # Open connection to ESXi host try: si = connect.SmartConnectNoSSL(host=host, user=ESXI_USERNAME, pwd=ESXI_PASSWORD) print("Connected to {}".format(host)) except: print("Failed to connect to {}".format(host)) inv = si.RetrieveContent() dc1 = inv.rootFolder.childEntity[0] vmList = dc1.vmFolder.childEntity for vm in vmList: # More details can be accessed from vm.summary, but these are the most relevant powerState = vm.summary.runtime.powerState bootTime = str(vm.summary.runtime.bootTime) #maxCpuUsage = vm.summary.runtime.maxCpuUsage
vm_col += 1 for item in hw_header: worksheet_hosts.write(hw_row, hw_col, item, bold) hw_col += 1 # Set row to start after headers vm_row = 1 hw_row = 1 for host in hosts: # Open connection to ESXi host try: si = connect.SmartConnectNoSSL(host=host, user=host_username, pwd=host_password) print("Connected to {}".format(host)) except: print("Failed to connect to {}".format(host)) inv = si.RetrieveContent() dc1 = inv.rootFolder.childEntity[0] vmList = dc1.vmFolder.childEntity for vm in vmList: # More details can be accessed from vm.summary, but these are the most relevant powerState = vm.summary.runtime.powerState bootTime = str(vm.summary.runtime.bootTime) #maxCpuUsage = vm.summary.runtime.maxCpuUsage