I want to build an automated script which builds an new VPS Server on the page vultr.com. They also have an API Access and I could create it and receive also anything I need. The problem is, vultr sends an response with a number as the key of every VPS Server. I used this command to create the VPS and get the number of the server back:
idofserver=$(curl -H 'API-KEY: "HERE IS MY PRIVATE API KEY"' \
https://api.vultr.com/v1/server/create \
--data 'DCID=9' --data 'VPSPLANID=201' --data 'OSID=244' \
| jq '."SUBID"' | /bin/sed 's/"//g')
And it also gives me back the number without the ". So the response in the command line looks like: 2342738. And the variable also got changed to the number. Because when I enter "echo $idofserver", I got the number 2342738 back.
After that line I do in a loop that command:
echo "VULRT IS WORKING ID: $idofserver"
response=$(curl -H 'API-KEY: "HERE IS MY PRIVATE API KEY"' \
https://api.vultr.com/v1/server/list 100>/dev/null)
status=$(echo "$response" | jq '."$idofserver"' | jq '."status"')
It also returns "VULTR IS WORKING ID: 2342738". But it stays in a complete loop, because it doesn't change the variable $status to active. The while loop condition is: while [ $status != "active" ]; do
I tried it manually to echo the response and send it through jq
with the commands from above $status. But there was the response: "null".
I don't know why.
Here is how the JSON Response looks like from VULTR:
{"2342738":{"SUBID":"2342738","os":"Debian 9 x64 (stretch)","ram":"1024 MB","disk":"Virtual 25 GB","main_ip":"11.11.11.11","vcpu_count":"1","location":"Frankfurt","DCID":"9","default_password":"=*{#?HHH*!-(","date_created":"2019-04-22 17:49:28","pending_charges":"0.01","status":"active","cost_per_month":"5.00","current_bandwidth_gb":0,"allowed_bandwidth_gb":"1000","netmask_v4":"255.255.0.0","gateway_v4":"11.11.11.11","power_status":"running","server_state":"installingbooting","VPSPLANID":"201","v6_main_ip":"","v6_network_size":"","v6_network":"","v6_networks":[],"label":"","internal_ip":"","kvm_url":"https:\/\/my.vultr.com\/subs\/vps\/novnc\/api.php?data=jhkjhjhkjhkj","auto_backups":"no","tag":"","OSID":"244","APPID":"0","FIREWALLGROUPID":"0"}}