vcenter rest api curl examples
The VMware vCenter Server APIs are organized around REST. For ease of use and security, REST builds on the standard web protocols HTTP and HTTPS, using the normal network ports 80 and 443, which are both open in most data centers, and uses standard HTTP response codes, authentication, and verbs.
Create a rest api session using curl
[root@localhost ~]# curl -s -k -X POST https://192.168.75.200/rest/com/vmware/cis/session -u 'administrator@vsphere.local':'Qwer&1234'
{"value":"ed5001263a1b9f65c961e63926d648ca"}
Then you can use the session ID “ed5001263a1b9f65c961e63926d648ca” to run other rest api command via curl
List all VMs using curl
[root@localhost ~]# curl -s -k -X GET -H "vmware-api-session-id: ed5001263a1b9f65c961e63926d648ca" https://192.168.75.200/rest/vcenter/vm | jq
{
"value": [
{
"memory_size_MiB": 1024,
"vm": "vm-23",
"name": "vm02",
"power_state": "POWERED_ON",
"cpu_count": 1
},
{
"memory_size_MiB": 1024,
"vm": "vm-30",
"name": "vm03",
"power_state": "POWERED_ON",
"cpu_count": 1
}
]
}
For more curl rest api commands you can refer:
https://192.168.75.200/apiexplorer
replace the address with your vcenter IP or DNS name.
Below is a script which using rest api to search VMs , or rest/poweroff/poweron VMs ,for reference only.
#!/bin/bash
vcenter="192.168.75.200"
show_help(){
echo "Usage: ${0##*/} [-h|--help] [-u|--user] [-f|--file filename] [-a|--action reset|start|stop] [vmname...] "
}
while (( "$#" )); do
case $1 in
-h|--help)
show_help
exit
;;
-f|--file)
if [ "$2" ] && [ ${2:0:1} != "-" ]; then
filename=$2
shift
else
echo "Option -f|--file needs an argument"
show_help
exit 1
fi
;;
-u|--user)
if [ "$2" ] && [ ${2:0:1} != "-" ]; then
USERNAME=$2
shift
else
echo "Option -u|--user needs an argument"
show_help
exit 1
fi
;;
-a|--action)
if [ "$2" ] && [ ${2:0:1} != "-" ]; then
action=$2
shift
else
echo "Option -a|--action needs an argument"
show_help
exit 1
fi
;;
-?*)
echo "Option $1 is not supported."
show_help
exit 1
;;
*)
break
esac
shift
done
if [ "$#" -eq 0 ] && [ "$filename" == "" ]; then
show_help
exit 1
fi
input=( "$@" )
if [ -f $filename ] && [ "$filename" != "" ]; then
echo "$filename test..."
while read line; do
input+=("$line")
done <$filename
#else
#echo "Error:$filename does not exist."
#exit 1
fi
vmfound=()
vmidfound=()
if [ "$USERNAME" == "" ]; then
read -p "Enter your username: " USERNAME
fi
makesession=`curl -s -k -X POST https://$vcenter/rest/com/vmware/cis/session -u "$USERNAME"`
if [[ "$makesession" =~ \{\"value.?* ]]; then
sessionid=`echo $makesession | awk -F\" '{print $(NF-1)}'`
else
echo "Error: Can not connect to vcenter"
exit 1
fi
for vmname in "${input[@]}"
do
vminfo=( $(curl -s -k -X GET -H "vmware-api-session-id: $sessionid" "https://$vcenter/rest/vcenter/vm?filter.names=$vmname" | jq -r '.[] | .[] | .name + " " + .vm ') )
if [ ${#vminfo[@]} -gt 0 ]; then
echo "${vminfo[0]}"
vmfound+=("${vminfo[0]}")
vmidfound+=("${vminfo[1]}")
fi
done
echo "${#input[@]} vmnames inputted , ${#vmfound[@]} vms found"
if [ "$action" == "reset" ] || [ "$action" == "start" ] || [ "$action" == "stop" ]; then
if [ ${#vmidfound[@]} -gt 0 ]; then
for i in "${!vmidfound[@]}"
do
echo "Power ${action}ing ${vmfound[i]}"
curl -s -k -X POST -H "vmware-api-session-id: $sessionid" "https://$vcenter/rest/vcenter/vm/{${vmidfound[i]}}/power/${action}"
sleep 3
done
fi
fi