python list all vm with ip on vsphere

2.2k views Asked by At

I'm trying to get the vm name and ip from my vsphere server, currently I have only the name of the vm. How to get the ip?

#! /usr/bin/env python
from pyVim.connect import SmartConnect, SmartConnectNoSSL, Disconnect
from pyVmomi import vim
import json

from pyVim.connect import SmartConnect
from pyVmomi import vim
import ssl

# Get all the Vms from vCenter server inventory and print its name
# Below is Python 2.7.x code, which can be easily converted to python 3.x version

s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode=ssl.CERT_NONE
si= SmartConnectNoSSL(host="192.168.1.2", user="[email protected]", pwd="my_pass")
content=si.content

# Method that populates objects of type vimtype
def get_all_objs(content, vimtype):
        obj = {}
        container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
        for managed_object_ref in container.view:
                obj.update({managed_object_ref: managed_object_ref.name})
        return obj

#Calling above method
getAllVms=get_all_objs(content, [vim.VirtualMachine])

#Iterating each vm object and printing its name
for vm in getAllVms:
        print (vm.name)
1

There are 1 answers

1
Kyle Ruddy On

The IP address information for each VM is located in the GuestInfo object, which would be available at: vm.guestinfo.ipaddress

Note, the IP address returned is sourced from VMware Tools so make sure those are installed and running.