-
Notifications
You must be signed in to change notification settings - Fork 0
/
getvnicinfo.py
128 lines (111 loc) · 4.45 KB
/
getvnicinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
#
# cpaggen - May 16 2015 - Proof of Concept (little to no error checks)
# - rudimentary args parser
# - GetHostsPortgroups() is quite slow; there is probably a better way
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import atexit
import sys
def GetVMHosts(content):
print("Getting all ESX hosts ...")
host_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.HostSystem], True)
obj = [host for host in host_view.view]
host_view.Destroy()
return obj
def GetVMs(content):
print("Getting all VMs ...")
vm_view = content.viewManager.CreateContainerView(content.rootFolder,
[vim.VirtualMachine], True)
obj = [vm for vm in vm_view.view]
vm_view.Destroy()
return obj
def GetHostsPortgroups(hosts):
print("Collecting portgroups on all hosts. This may take a while ...")
hostPgDict = {}
for host in hosts:
pgs = host.config.network.portgroup
hostPgDict[host] = pgs
print("\tHost {} done.".format(host.name))
print("\tPortgroup collection complete.")
return hostPgDict
def PrintVmInfo(vm):
vmPowerState = vm.runtime.powerState
print("Found VM:", vm.name + "(" + vmPowerState + ")")
GetVMNics(vm)
def GetVMNics(vm):
# hosts is global - this avoids passing the list each time the function is called
# which is once per VM
for dev in vm.config.hardware.device:
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
dev_backing = dev.backing
if hasattr(dev_backing, 'port'):
portGroupKey = dev.backing.port.portgroupKey
dvsUuid = dev.backing.port.switchUuid
try:
dvs = content.dvSwitchManager.QueryDvsByUuid(dvsUuid)
except:
portGroup = "** Error: DVS not found **"
vlanId = "NA"
vSwitch = "NA"
else:
pgObj = dvs.LookupDvPortGroup(portGroupKey)
portGroup = pgObj.config.name
vlanId = str(pgObj.config.defaultPortConfig.vlan.vlanId)
vSwitch = str(dvs.name)
else:
portGroup = dev.backing.network.name
vmHost = vm.runtime.host
# global variable hosts is a list, not a dict - I can't access it by key
host_pos = hosts.index(vmHost)
viewHost = hosts[host_pos]
# global variable hostPgDict stores portgroups per host
pgs = hostPgDict[viewHost]
for p in pgs:
if portGroup in p.key:
vlanId = str(p.spec.vlanId)
vSwitch = str(p.spec.vswitchName)
if portGroup is None:
portGroup = 'NA'
print(
'\t' + dev.deviceInfo.label + '->' + dev.macAddress + ' @ '
+ vSwitch + '->' + portGroup + ' (VLAN ' + vlanId + ')')
def login():
if len(sys.argv) != 4:
host = raw_input("vCenter IP: ")
user = raw_input("Username: ")
password = raw_input("Password: ")
else:
host, user, password = sys.argv[1:]
return host, user, password
def main():
global content, hosts, hostPgDict
host, user, password = login()
serviceInstance = SmartConnect(host=host,
user=user,
pwd=password,
port=443)
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
hosts = GetVMHosts(content)
hostPgDict = GetHostsPortgroups(hosts)
vms = GetVMs(content)
for vm in vms:
PrintVmInfo(vm)
# Main section
if __name__ == "__main__":
sys.exit(main())