Invoking a VNC Console From Bash using Proxmox API

Zubin Singh Parihar

Well-Known Member
Nov 16, 2017
63
10
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.
 
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.
Hi @nosoop4u are you already Authenticated to Proxmox in that browser though?
 
Code:
https://<HOST>:8086/?console=kvm&novnc=1&vmid=102&vmname=nginx-gui&node=innprox&resize=off&cmd=&vncticket=PVEVNC%3A6780AB52%3A%3AsqhsA%2B2ZCDfnnazo2ge33nxYyJW76XmCROP0h0o4JJ4Py2Wwu8H5Qw36T%2BNzAdOCaCvwaLgcauO5cWomeWT06bbFyHpPa5jubhXmfe2hnDNU14%2B2HS3SMPGiLVgfrEyCPh8D213bNdMp3F7SWOZsONB6quIyWrfARZLKncXWXRVW4NP2kUtg%2B%2B2rfIopzt68s6cGlvdRbbSQQNKDnYg6iORs%2Fw4895CVRl%2FDOdfxIDGXFmJZcdJO0DF8lAJ8Cwocu2l2WC8q8ZbwVQTIb8sXRNldi95xjPCOpoq84OUF1n99QqwibV%2FtAvflySH0hzX9VNCX%2Bzt2gP2m%2F3pVR8urXA%3D%3D


This is my Encoded URL I am trying to use but it show Error 401 NO Ticket in the NoVNC UI.
Note:- I am trying this url in incognito tab. If i set the PVEAuthCookie manually it works
 
Last edited: