[TUTORIAL] API automation, Power ON/OFF vm and else.

Veeh

Active Member
Jul 2, 2017
49
6
28
35
Dear Proxmox community,

I have been working on this for the last few days. And It might help some of you so I decided to share it.
My home setup has VM, that are not on all the time. I was using WOL + python script previously to power on VM.
With the upgrade to proxmox7, I was not working anymore, and instead of installing python2 lib, I preferred to embrace the API way.

So there it is.
The 2 following scripts, (Powershell and Bash) are sending a request to get a ticket.
then they are reusing the data from the ticket to do whatever you put in the endpoint!

Enjoy.

PowerShell:
Code:
##################################
### API AUTOMATION
# for the proxmox community
# By veeh, enjoy
#
# Requirement: not working with Powershell5.1, I did this with powershell7.

# Host info
$pve = "HOST_FQDN_OR_IP"
$node = "HOSTID"
$port = ":8006"
$vmid = "VMID"

# API info
$apiu = "user@realm"
$apip = "USER_PASSWORD"
$url_base = "https://$pve$port/api2/json"
# this is where you put what ever you want do
# https://pve.proxmox.com/pve-docs/api-viewer/
$url_end = "/nodes/$node/qemu/$vmid/status/start"
$urlqr = $url_base + $url_end
$urltk = "$url_base/access/ticket"

# Ticket creation
$ticket = curl --insecure --data "username=$apiu&password=$apip" $urltk

# Parse the ticket data to grab the cookie and token
$Array = $ticket.Split('"')
# Grab the cookie
$linecookie = $Array | select-string ticket
$linecookienb = $linecookie.LineNumber + 1
$cookie = $Array | select -Index $linecookienb
# Grab the token
$linetoken = $Array | select-string CSRFP
$linetokennb = $linetoken.LineNumber + 1
$token = $Array | select -Index $linetokennb

# proxmox api query
$headersps = @{
    "cookie" = "PVEAuthCookie=$cookie"
    "CSRFPreventionToken" = "$token"
}
Invoke-RestMethod -SkipCertificateCheck -Uri $urlqr -Method Post -Headers $headersps

Bash:
Code:
#!/bin/bash
##################################
### API AUTOMATION
# for the proxmox community
# By veeh, enjoy

#Host info
pve="HOST_FQDN_OR_IP"
node="HOSTID"
port=":8006"
vmid="VMID"

#API info
apiu="user@realm"
apip="USER_PASSWORD"
url_base="https://$pve$port/api2/json"
# this is where you put what ever you want do
# https://pve.proxmox.com/pve-docs/api-viewer/
url_end="nodes/$node/qemu/$vmid/status/stop"
urlqr="$url_base/$url_end"
urltk="$url_base/access/ticket"

ticket=`curl --insecure --data "username=$apiu&password=$apip" $urltk`

# Grab cookie and token from the ticket data
cookieid=`echo $ticket | tr -t '"' '\n' | grep "PVE:api@pve"`
cookie="PVEAuthCookie=$cookieid"
ticketid=`echo $cookie | awk -F ':' '{ print $3 }'`
tokenid=`echo $ticket | tr -t '"' '\n' | grep $ticketid | grep -v PVE`
token="CSRFPreventionToken:$tokenid"

#proxmox api query
curl --insecure --cookie $cookie --header $token -X POST "$urlqr"
 
Last edited:

Norbertt

New Member
Feb 9, 2022
1
0
1
33
Hello,

I am not a expert so please could you explain to me a bit more about this script?

The bash code should be running from the device which will send the wake up command to the VM?

Which data should I change from your bash ?

Thank you
 

Veeh

Active Member
Jul 2, 2017
49
6
28
35
Hi,

This script is using the API system available with proxmox 6.7 if I remember correctly (Available in version 7 that's for sure).

What you need to feed is the host info and API credentials.
pve="HOST_FQDN_OR_IP"
node="HOSTID"
port=":8006"
vmid="VMID"
apiu="user@realm"
apip="USER_PASSWORD"

First, you need to create an account to use the API system.
Datacenter > Permissions > API Tokens
then add your user. this is your API credentials.
https://pve.proxmox.com/wiki/Proxmox_VE_API

The script is creating a ticket to run an action.
The action is defined by $url_end
url_end="nodes/$node/qemu/$vmid/status/stop" (in this case that action will stop the VM $vmid)

If your API user has the proper permission you can run whatever you want as long as it is listed here:
https://pve.proxmox.com/pve-docs/api-viewer/index.html
another example: nodes/{node}/status/reboot (this will reboot your host)

the base URL for your API is always the same https://HOSTIP:8006/api2/json
It is what comes after that will tell the system what it has to do.

The last bit will basically grab in the ticket what is required to be able to trigger the API action.

In summary, this is how it works. Your API user allows you to get a ticket, this ticket will have the necessary information to trigger the API action requested as long as your user has the right permissions.

You can run this from any device. I use it on my laptop to wake up/reboot/shut down VM on my workstation.
But you can also run this from your cellphone. With an app sending HTTP POST requests.
It's a little bit more complicated because you need a token. But it's working fine.
 

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 your own in 60 seconds.

Buy now!