#!/bin/bash
# needs pve-manager >= 3.1-44
usage() {
echo "Usage: $0 [-u <string>] [-p <string>] vmid node proxy"
echo
echo "-u username. Default root@pam"
echo "-p password. Default ''"
echo
echo "node: Proxmox cluster node name"
echo "proxy: DNS or IP"
echo "vmid: id for VM"
exit 1
}
while getopts ":u:p:" o; do
case "${o}" in
u)
USERNAME=${OPTARG}
;;
p)
PASSWORD=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z $PASSWORD ]; then
PASSWORD=""
fi
if [ -z $USERNAME ]; then
USERNAME=root@pam
fi
# select VM
[ -z $1 ] && usage
VMID=$1
[ -z $2 ] && usage
NODE=$2
# or NODE=${2:-<default node to use>}
[ -z $3 ] && usage
PROXY=$3
# or PROXY=${3:-<default proxy to use>}
DATA=`curl -s -k -d "username=$USERNAME&password=$PASSWORD" https://$PROXY:8006/api2/json/access/ticket`
TICKET=`echo $DATA|sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"// g'|grep -w ticket| awk -F "|" '{print $2}'`
CSRF=`echo $DATA|sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"// g'|grep -w CSRFPreventionToken| awk -F "|" '{print $2}'`
curl -s -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" https://$PROXY:8006/api2/spiceconfig/nodes/$NODE/qemu/$VMID/spiceproxy -d "proxy=$PROXY" > spiceproxy
remote-viewer spiceproxy 2>&1 > /dev/null 2>&1