Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import sys
import json
import requests
args = sys.argv
vm_name = args[1]
cvm_ip = 'NutanixクラスタVIP'
cvm_user = 'ユーザ'
cvm_pass = 'パスワード'
if __name__ == '__main__':
base_url = 'https://' + cvm_ip + ':9440/PrismGateway/services/rest/v2.0/'
requests.packages.urllib3.disable_warnings()
s = requests.Session()
s.auth = (cvm_user, cvm_pass)
s.headers.update({'Content-Type': 'application/json; charset=utf-8'})
api_url = base_url + 'vms'
vms = s.get(api_url, verify=False).json()
for vm in vms['entities']:
if vm['name'] == vm_name:
vm_uuid = vm['uuid']
api_url = base_url + '/vms/' + vm['uuid'] + '/nics'
vnics = s.get(api_url, verify=False).json()
for vnic in vnics['entities']:
vnic_mac = vnic['mac_address']
if vnic.get('requested_ip_address'):
vnic_ip = vnic['requested_ip_address']
else:
api_url = base_url + 'networks/' + vnic['network_uuid'] + '/addresses'
vnic_nw_addrs = s.get(api_url, verify=False).json()
for vnic_nw_addr in vnic_nw_addrs['entities']:
if vnic_nw_addr['mac_address'] == vnic_mac:
if vnic_nw_addr.get('ip_address'):
vnic_ip = vnic_nw_addr['ip_address']
print vm_uuid + ',' + vnic_mac + ',' + vnic_ip + ',' + vm_name
[root@jbox01 ~]# python ntnx-vm-ip.py vm03
d38785cd-45ec-48b9-b26f-5f26c7f011bf,50:6b:8d:f8:78:1a,192.168.1.223,vm03
d38785cd-45ec-48b9-b26f-5f26c7f011bf,50:6b:8d:06:81:6a,192.168.12.179,vm03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment