Inventory with API | Nutanix Community
Skip to main content
Hi,

Is there a way to see/export the Hostname and IP of the VMs with the API via cURL?

Anybody have an example o extract this information?



Thanks

Regards
cURL isn't really meant to be used as a filter mechanism but you can achieve this by setting the cURL output to a variable then using egrep to pull out the strings that you want.



The below should get you the information filtered down from the whole JSON that is returned. I tested this with V2 of the API:

/PrismGateway/services/rest/v2.0/vms/?length=1&include_vm_nic_config=true



code:
output=$(curl -k -X GET   'https://{{IP_ADDRESS}}:9440/PrismGateway/services/rest/v2.0/vms/?length=1&include_vm_nic_config=true' -H 'Authorization: Basic {{AuthGuid}}=')

echo $output | egrep -o '"name": *"[^"]*"|"ip_address": *"[^"]*"'



I would really suggest using Python or the JQ Module to parse the Key Value pairs for JSON output rather than trying to egrep it. It would be much cleaner:



https://stedolan.github.io/jq/
thanks for the answer @rhunt

Is it possible to extract with sed the content of 'ipAddresses' ? for example:



"hypervisorType": "kKvm",

"ipAddresses": [

"172.10.10.10"

],

"memoryCapacityInBytes": 4294967296,
sed is going to be a little more difficult. You will have to use substitution to print ONLY the data you want and not the entire line. I have only ever used sed for file line replacements.



I THINK the regex for sed would look something like this to get exactly what you have listed above. Untested:



code:
\"hypervisorType\": \"[a-zA-Z0-9]+\",\"ipAddresses\": \[\"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\"\],\"memoryCapacityInBytes\": \d+

@guif



Please find the script



https://github.com/sandeepmp/nutanix/blob/master/get-vm-details.ps1