Shutdown Proxmox using APi

Surgical

New Member
Jul 21, 2021
4
0
1
29
Hi Guys

So long story short I want to shut down my physical server that is running Proxmox VE using the API. I am fairly new to using API but what I have found thus far is this:
curl https://yourip:8006/api2/json//nodes/proxmox/stop -k -d 'username=root@pam&password=yourpassword'

Above does not seem to work, Anyone able to assist and just provide the correct curl to use

Thanx
 
Please don't use the root user for that. It is possible to create a new user, name it "shutdown" or something like that, create a new role like "shutdown", add the "shutdown" role to the "shutdown" user. If you then only give that role the rights to use "Sys.PowerMgmt" that user is only allowed to start/shutdown/reboot the node.
Never a good idea to use the root user and its password in clear text. The password could end up in logs if the command fails to execute and so on.
 
Last edited:
Please don't use the root user for that. It is possible to create a new user, name it "shutdown" or something like that, create a new role like "shutdown", add the "shutdown" role to the "shutdown" user. If you then only give that role the rights to use "Sys.PowerMgmt" that user is only allowed to start/shutdown/reboot the node.
Never a good idea to use the root user and its password in clear text. The password could end up in logs if the command fails to execute and so on.
Noted thanx man. But do you know what Command I should use? Above post did not work
 
You could try it with this:
Code:
curl 'https://REPLACEMEWITHIPORHOSTNAME:8006/api2/extjs/nodes/NODENAME/status' -H 'Cookie: PVEAuthCookie=REPLACEMEWITHCORRECTCOOKIE' -H 'Content-Type: application/x-www-form-urlencoded' -H 'CSRFPreventionToken: REPLACEMEWITHCORRECTTOKEN' --data 'command=shutdown' --insecure
And by using tokens you don't got the problem with the passwords.
 
Last edited:
Hey Man. No it did not seem to work. I don't know where to find the cookie. So to give more context I want to run this command from a terminal in Home Assistant as I'm using automation to turn my device off before scheduled power outages in my area. And I need to get this to work before I can Migrate all my VM's over from VMWARE Workstation to Proxmox lol
 
just my 5 cents, but ..

If a scheduelled power-outage were there, would a scheduelled cronjob on the Proxmox host be equally efficient, without poking around with the API ?

- if the scheduelled outage is always the same, then use a cronjob to shutdown the hosts/guests prior to the outage happens.
- if the outage is scheduelled on different times you can use the at daemon to schedule it ( this has my preference cause the job gets cleared after execution, meaning per occurane you will have to reschedule it.

or am i understanding the case as presented wrongly ?
 
Last edited:
  • Like
Reactions: gentoomaniac
just my 5 cents, but ..

If a scheduelled power-outage were there, would a scheduelled cronjob on the Proxmox host be equally efficient, without poking around with the API ?

- if the scheduelled outage is always the same, then use a cronjob to shutdown the hosts/guests prior to the outage happens.
- if the outage is scheduelled on different times you can use the at daemon to schedule it ( this has my preference cause the job gets cleared after execution, meaning per occurane you will have to reschedule it.

or am i understanding the case as presented wrongly ?
This would work yes, but the times are not set. Sometimes weeks go by before power outages would occur. I live in South Africa so we get Load shedding (Then Power load is too high we receive 2 hours load shedding where we do not have power for 2 hours) now I have a system in place to run automation based on this. So far it turns off my server that is running Windows Server 2019, But if I move over to proxmox I need to also be able to do the same
 
This would work yes, but the times are not set. Sometimes weeks go by before power outages would occur. I live in South Africa so we get Load shedding (Then Power load is too high we receive 2 hours load shedding where we do not have power for 2 hours) now I have a system in place to run automation based on this. So far it turns off my server that is running Windows Server 2019, But if I move over to proxmox I need to also be able to do the same
Did you manage to get this working? Also from SA and want to know how to achieve this, possibly with the ESP API that could be used to enhance this? Your feedback would be much appreciated
 
the initial post was almost correct, but
- if you want to use a user+password, you need to first get a ticket and then pass the ticket as cookie to the API call
- if you want to use an API token, you just need to pass the API token as Authorization header

https://pve.proxmox.com/pve-docs/pve-admin-guide.html#pveum_tokens
https://pve.proxmox.com/wiki/Proxmox_VE_API
Thank you for your response, it is much appreciated. Just to clarify (since I honestly had/have no idea on how to get a ticket and pass is a cookie to the api call, feel like Im out of my depth here)
Will I need to follow the steps in this video to accomplish this? https://www.youtube.com/watch?v=Iuyd2TXe8SA
What im trying to do is basically shutdown the server from home assistant when power outages are expected. The schedule changes on a daily basis based on the severity of the national power constraints. From home assistant I am able to retrieve the upcoming schedule and trigger commands accordingly..

Any assistance would be highly appreciated
 
Something like:
Code:
# shutdown a remote PVE server using the PVE API (Sys.PowerManagement privilege needed):
curl -k -X POST -H 'Authorization: PVEAPIToken=<ApiToken>=<TokenSecret>' https://<PveIP>:8006/api2/json/nodes/<NodeName>/status/shutdown
 
  • Like
Reactions: fabian
Something like:
Code:
# shutdown a remote PVE server using the PVE API (Sys.PowerManagement privilege needed):
curl -k -X POST -H 'Authorization: PVEAPIToken=<ApiToken>=<TokenSecret>' https://<PveIP>:8006/api2/json/nodes/<NodeName>/status/shutdown
So, ive been trying most of this weekend but for some reason it just refuses to to work. I'm really not sure what I'm doing wrong or not doing.
I've followed the youtube video I mentioned in my previous post, got the token and csrf from postman, tried all 3 different variations in this thread, but no luck whatsoever...
 
So, ive been trying most of this weekend but for some reason it just refuses to to work. I'm really not sure what I'm doing wrong or not doing.
I've followed the youtube video I mentioned in my previous post, got the token and csrf from postman, tried all 3 different variations in this thread, but no luck whatsoever...
The following works for me:
1. Create an API token with User Name & Token ID, making sure Privilege Separation is NOT checked.
2. Copy User, Token ID & Token Secret carefully.
2. Use the following command to shutdown, taking care with all the quotation marks & spaces. Also note the exclamation mark between User Name and Token ID:

curl 'https://<IP>:8006/api2/extjs/nodes/<NodeName>/status' -H 'Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>' -H 'Content-Type: application/x-www-form-urlencoded' --data 'command=shutdown' --insecure

Note you can also replace 'command=shutdown' to 'command=reboot' to do just that.

Hope this helps you.
 
Last edited:
The following works for me:
1. Create an API token with User Name & Token ID, making sure Privilege Separation is NOT checked.
2. Copy User, Token ID & Token Secret carefully.
2. Use the following command to shutdown, taking care with all the quotation marks & spaces. Also note the exclamation mark between User Name and Token ID:

curl 'https://<IP>:8006/api2/extjs/nodes/<NodeName>/status' -H 'Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>' -H 'Content-Type: application/x-www-form-urlencoded' --data 'command=shutdown' --insecure

Note you can also replace 'command=shutdown' to 'command=reboot' to do just that.

Hope this helps you.
Thank you! this works perfectly via terminal, but for some reason I keep on getting "bad indentation of mapping entries" errors. Do you think I should seek help in the homeassitant forums?
 
Thank you! this works perfectly via terminal, but for some reason I keep on getting "bad indentation of mapping entries" errors. Do you think I should seek help in the homeassitant forums?
In Home Assistant when you save it as a shell command, you need to enclose the whole command (from before the word curl till after insecure) in single quotes. Then everywhere where there is currently a quote replace it with double quotes.
So you should end up with this for example:

'curl "https://<IP>:8006/api2/extjs/nodes/<NodeName>/status" -H "Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>" -H "Content-Type: application/x-www-form-urlencoded" --data "command=shutdown" --insecure'
 
Last edited:
  • Like
Reactions: neoplayer
In Home Assistant when you save it as a shell command, you need to enclose the whole command (from before the word curl till after insecure) in single quotes. Then everywhere where there is currently a quote replace it with double quotes.
So you should end up with this for example:

'curl "https://<IP>:8006/api2/extjs/nodes/<NodeName>/status" -H "Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>" -H "Content-Type: application/x-www-form-urlencoded" --data "command=shutdown" --insecure'
Sorry for late response...this worked perfectly! Thank you!
 
  • Like
Reactions: neoplayer
I know that this is a post for shuttting down the PVE host via the Rest API, but I tried adapting the solution of user @gfngfn256 to shutdown a VM on the node, following the Proxmox documentation I just had to make a few modifications. This was the result:
Bash:
'curl "https://$IP:$PORT/api2/extjs/nodes/{node}/stopall" -H "Authorization: PVEAPIToken=USER@REALM!TOKENID=UUID" -H "Content-Type: application/x-www-form-urlencoded" --data "vms=2201" --insecure'

But it returns the message: Permission check failed.
I have already verified that I'm using the correct Token and Token Secret by running the @gfngfn256 solution successfully and I also verified that VM.PowerMgmt Permission is correctly linked to the API Token.

I would appreciate any ideas on what the problem could be.

Thanks!
 
Last edited:
1. if you only want to shutdown a single VM, use " POST /api2/json/nodes/{node}/qemu/{vmid}/status/shutdown "
2. if you get permission check failed, you are lacking permissions - check with "pveum" that your token actually has the required ones..
 
yes, because you token doesn't have the right permission.. you need 'VM.PowerMgmt' on '/vms/XXX'
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!