-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnic.inventory.py
50 lines (38 loc) · 1.36 KB
/
nic.inventory.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
#! /usr/bin/python
# NIC inventory
# Anis Kochlef
# 10/02/2020
# akochlef@gmail.com
import json
import requests
import getpass
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
_SESSION = requests.Session()
_SESSION.verify=False
_SESSION_STATUS = 0
vc = input('vCenter IP or FQDN: ')
username = input('Username: ')
password = getpass.getpass()
url = f'https://{vc}/rest/com/vmware/cis/session'
_SESSION.post(url,auth=(username,password))
def nic_info(session,server,vmid):
url = f'https://{vc}/rest/vcenter/vm/{vmid}'
r = session.get(url)
j = json.loads(r.text)
nics = j['value']['nics']
name = j['value']['name']
power_state = j['value']['power_state']
for nic in nics:
info = f"{name},{power_state},{nic['value']['label']},{nic['value']['state']},{nic['value']['mac_address']}"
print(info)
def inventory(session,server):
url = f' https://{server}/rest/vcenter/vm'
response = session.get(url)
data = json.loads(response.text)
vms=data['value']
for vm in vms:
vmid = vm['vm']
nic_info(session,server,vmid)
inventory(_SESSION,vc)
# https://vdc-download.vmware.com/vmwb-repository/dcr-public/423e512d-dda1-496f-9de3-851c28ca0814/0e3f6e0d-8d05-4f0c-887b-3d75d981bae5/VMware-vSphere-Automation-SDK-REST-6.7.0/docs/apidocs/operations/com/vmware/vcenter/vm.get-operation.html