0

My program is running inside a VMware virtual machine, my purpose is to get some information about the machine which this vm is hosted on.

I've already done some googling and find a library called pyVmomi.

But I still can't figure out how to get the information I want.

The samples are almost all about getting all vms or all hosts, and there is not obvious way I can adapt them for getting information about the current machine.

satoru
  • 31,822
  • 31
  • 91
  • 141
  • What platform is the current VM hosted on? Is it a pay version of vSphere with an API license? – Michael Rice Feb 02 '16 at 02:11
  • @MichaelRice Yes, I have the API license. – satoru Feb 02 '16 at 03:23
  • So then you want to hit the vSphere API, then get the details for a given VM (based on name, or uuid?), and the details you want are hostname and IP? – Michael Rice Feb 02 '16 at 08:18
  • @MichaelRice The program will be running inside a VM, I don't know how to get the name or UUID of the "current" VM. – satoru Feb 02 '16 at 08:29
  • What youre asking for is confusing to say the least. But if I understand this correctly: You have vSphere. Running in vSphere on some ESXi host you have a VM. In that VM you want to run a script. You want that script to get info about the ESXi host that your VM is currently running on. The info you want is the name of the ESXi host, and its IP? – Michael Rice Feb 02 '16 at 08:48
  • @MichaelRice From the pyVmomi library we can get a `HostSystem` object, from which we can get many properties of the "ESXi host" (or machine hosting my vm). But I don't know how to find this `HostSystem` in the first place. – satoru Feb 02 '16 at 09:13

2 Answers2

1

Assuming your VM (that is running this pyVmomi script) is running some version of Linux you could use something like dmidecode to find the UUID.

import subprocess

from pyVim import connect

proc = subprocess.Popen(["sudo dmidecode|grep UUID|awk '{print $2}'"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
uuid = out[:-1]

SI = None
SI = connect.SmartConnect(host=ARGS.host,
                          user=ARGS.user,
                          pwd=ARGS.password,
                          port=ARGS.port)

VM = SI.content.searchIndex.FindByUuid(None, uuid,
                                       True,
                                       False)

HOST = VM.runtime.host

print "Host name: {}".format(HOST.name)

What this will do is execute a system command on the Linux box to find the UUID. VMWare uses the BIOS UUID as the default UUID so dmidecode should work here. Next it will connect to a given vSphere host (in this example I assume a vCenter, but an ESXi host should provide the same results here). Next it will search the inventory looking for a VM with a matching UUID. From there it calls the runtime.host method which will return the HostSystem for the VM. Please note due to clustering that host could change.

Michael Rice
  • 7,974
  • 1
  • 15
  • 18
-1

This should help, install pynetinfo and pass device to the function

#!/usr/bin/python
import netinfo

def get_route( interface ):
    r = []
    for routes in netinfo.get_routes():
        if routes[ 'dev' ] == interface:
            r.append( routes[ 'dest' ] )
    return r

print get_route( 'wlan0' )
James John
  • 409
  • 2
  • 11