Invoking a VNC Console From Bash using Proxmox API

Zubin Singh Parihar

Well-Known Member
Nov 16, 2017
57
7
48
43
Hey Folks,

I'm needing a bit of help here and if someone can point me in the right direction, it would be greatly appreciated.

What I'm trying to do is create a bash script which has proxmox credentials and the VMID and from those, print a URL where by when I click it, it will open up a browser and be able to access the VNC console immediately (via a authentication ticket).

(I'm posting some dummy credential data)


This is what I have so far:
Bash:
#!/bin/bash

# Proxmox API settings
PROXMOX_HOST="192.168.103.21:8006"
API_USER="root@pam"
API_PASSWORD="abcde12345"
VM_ID=100

# Step 1: Log in and get the ticket and CSRFPreventionToken
response=$(curl -k -s "https://${PROXMOX_HOST}/api2/json/access/ticket" \
    -d "username=${API_USER}&password=${API_PASSWORD}" \
    -H "Content-Type: application/x-www-form-urlencoded")
TICKET=$(echo $response | jq -r '.data.ticket')
CSRF_TOKEN=$(echo $response | jq -r '.data.CSRFPreventionToken')
COOKIE="PVEAuthCookie=${TICKET}"

# Step 2: Get the VM's current node and VM name
node_response=$(curl -k -s "https://${PROXMOX_HOST}/api2/json/cluster/resources?type=vm" \
    --cookie "PVEAuthCookie=$TICKET")
NODE=$(echo $node_response | jq -r --arg VM_ID "$VM_ID" '.data[] | select(.vmid == ($VM_ID|tonumber)) | .node')
VMNAME=$(echo $node_response | jq -r --arg VM_ID "$VM_ID" '.data[] | select(.vmid == ($VM_ID|tonumber)) | .name')

# Step 3: Create a VNC ticket specifically for VNC access
vnc_ticket_response=$(curl -k -s "https://${PROXMOX_HOST}/api2/json/nodes/$NODE/qemu/$VM_ID/vncproxy" \
    --cookie "PVEAuthCookie=$TICKET" \
    -H "CSRFPreventionToken: $CSRF_TOKEN" -d '')
VNC_TICKET=$(echo $vnc_ticket_response | jq -r '.data.ticket')

# Step 4: Generate the VNC URL with the ticket
VNC_URL="https://${PROXMOX_HOST}/?console=kvm&novnc=1&vmid=${VM_ID}&vmname=${VMNAME}&node=${NODE}&resize=off&cmd=&vncticket=${VNC_TICKET}"

echo "Open the following URL in your browser to access the VNC console without prior authentication:"
echo $VNC_URL


I thought that this would work, however, it is resulting in "Error 401: No ticket"

Here is the resulting URL:

https://192.168.2.51:8006/?console=...Z0yapSElgVzbKSEUFuMPZvL0qpjdCwjmAnhETKg7H3g==

1726337322220.png

Would anybody be able to point me in the right direction? It feels so close.

Thanks!
 
FYI, I tried your script with 2 different Proxmox hosts (both at v8.0.3) and it worked for both. VNC console for multiple different VMs (tried different VM IDs) came up in Google Chrome 128.0.6613.120 (Official Build) (64-bit) from a Windows 10 client without any prompts for credentials.
 

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!