2014-03-03 10:22:56 +04:00
#!/bin/bash
2014-04-03 08:58:08 +04:00
set -e
2014-03-03 10:22:56 +04:00
# needs pve-manager >= 3.1-44
2014-04-03 08:41:02 +04:00
usage() {
echo "Usage: $0 [-u <string>] [-p <string>] vmid [node [proxy]]"
echo
echo "-u username. Default root@pam"
echo "-p password. Default ''"
echo
echo "vmid: id for VM"
echo "node: Proxmox cluster node name"
echo "proxy: DNS or IP (use <node> as default)"
exit 1
}
PASSWORD=""
USERNAME=""
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
DEFAULTHOST=$(hostname -f)
2014-03-03 10:22:56 +04:00
# select VM
2014-04-03 08:41:02 +04:00
[ -z $1 ] && usage
VMID=$1
#[ -z $2 ] && usage
NODE=${2:-$DEFAULTHOST}
if [ -z $3 ]; then
PROXY=$NODE
else
PROXY=$3
fi
2014-03-03 10:22:56 +04:00
2014-04-03 08:41:02 +04:00
NODE=${NODE%%\.*}
2014-03-03 10:22:56 +04:00
2014-04-03 08:58:08 +04:00
DATA=$(curl -f -s -S -k -d "username=$USERNAME&password=$PASSWORD" https://$PROXY:8006/api2/json/access/ticket)
echo "AUTH OK"
2014-03-03 10:22:56 +04:00
2014-03-17 15:00:00 +04:00
TICKET=${DATA//\"/}
TICKET=${TICKET##*ticket:}
TICKET=${TICKET%%,*}
2014-03-03 10:22:56 +04:00
2014-03-17 15:00:00 +04:00
CSRF=${DATA//\"/}
CSRF=${CSRF##*CSRFPreventionToken:}
CSRF=${CSRF%%,*}
2014-03-03 10:22:56 +04:00
2014-04-03 08:58:08 +04:00
curl -f -s -S -k -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" https://$PROXY:8006/api2/spiceconfig/nodes/$NODE/qemu/$VMID/spiceproxy -d "proxy=$PROXY" > spiceproxy
2014-03-03 10:22:56 +04:00
remote-viewer spiceproxy