Start/ Stop Container Using The Proxmox Web API in Bash
Category : How-to
The Proxmox Web API can perform any actions available in the front end Web. By implementing a REST API, all commands have been exposed and can be used programatically.
In this example we’ll use Bash to call the Proxmox Web API with our authentication token to start and stop an existing LXC Container.
See this post for an introduction to the Proxmox Web API, including all available API commands.
To issue API requests you’ll need to ensure you have already generated an authentication ticket which is described in Parse Proxmox Web API authentication ticket and the CSRFPreventionToken in Bash.
Once you have the authentication ticket you’ll need to call the Proxmox API using curl and parse the result. Use the below scripts and substitute the values as required:
Start an LXC Container
- TICKET is the authentication ticket that was produced in the Parse Proxmox Web API authentication ticket and the CSRFPreventionToken in Bash post. Ideally you would programatically call the authentication routine and then pass the values straight into the below API calls.
- CSRF is produced in the same way as TICKET. It’s actually only required when writing data to the API but there is no harm in always including it.
- HOST is the host or IP address of the Proxmox server.
- NODE is the node name of the Proxmox server that the LXC Container resides on.
- TARGET_VM is the VMID of the LXC Container.
TICKET=[security-ticket] CSRF=[csrf-token] HOST=prox-node1 NODE=prox-node1 TARGET_VMID=100 START_TASK_DATA=`curl -s -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" -X POST $HOST/api2/json/nodes/$NODE/lxc/$TARGET_VMID/status/start` START_TASK_RESULT=$(decodeDataFromJson $START_TASK_DATA 'data')
If $START_TASK_RESULT doesn’t come back with null or empty then the command has successfully executed.
Stop an LXC Container
Stopping a VM in Proxmox is very similar to starting one, with just a slight change to the API URL call. All other options are the same as the above section ‘Start an LXC Container’.
TICKET=[security-ticket] CSRF=[csrf-token] HOST=prox-node1 NODE=prox-node1 TARGET_VMID=100 STOP_TASK_DATA=`curl -s -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" -X POST $HOST/api2/json/nodes/$NODE/lxc/$TARGET_VMID/status/stop` STOP_TASK_RESULT=$(decodeDataFromJson $STOP_TASK_DATA 'data')
If $STOP_TASK_RESULT doesn’t come back with null or empty then the command has successfully executed.