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:
Bash:
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/start"
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:$apiu"`
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: