2014-03-03 07:22:56 +01:00
#!/bin/bash
2014-04-03 06:58:08 +02:00
set -e
2014-03-03 07:22:56 +01:00
# needs pve-manager >= 3.1-44
2014-04-03 06:41:02 +02: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)
2017-09-25 10:56:01 +02:00
USERNAME="${OPTARG}"
2014-04-03 06:41:02 +02:00
;;
p)
2017-09-25 10:56:01 +02:00
PASSWORD="${OPTARG}"
2014-04-03 06:41:02 +02:00
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
2017-09-25 10:56:01 +02:00
if [[ -z "$PASSWORD" ]]; then
2014-04-03 06:41:02 +02:00
PASSWORD=""
fi
2017-09-25 10:56:01 +02:00
if [[ -z "$USERNAME" ]]; then
USERNAME='root@pam'
2014-04-03 06:41:02 +02:00
fi
2017-09-25 10:56:01 +02:00
DEFAULTHOST="$(hostname -f)"
2014-04-03 06:41:02 +02:00
2014-03-03 07:22:56 +01:00
# select VM
2017-09-25 10:56:01 +02:00
[[ -z "$1" ]] && usage
VMID="$1"
2014-04-03 06:41:02 +02:00
2017-09-25 10:56:01 +02:00
#[[ -z "$2" ]] && usage
NODE="${2:-$DEFAULTHOST}"
2014-04-03 06:41:02 +02:00
2017-09-25 10:56:01 +02:00
if [[ -z "$3" ]]; then
PROXY="$NODE"
2014-04-03 06:41:02 +02:00
else
2017-09-25 10:56:01 +02:00
PROXY="$3"
2014-04-03 06:41:02 +02:00
fi
2014-03-03 07:22:56 +01:00
2017-09-25 10:56:01 +02:00
NODE="${NODE%%\.*}"
2014-03-03 07:22:56 +01:00
2017-09-25 10:56:01 +02:00
DATA="$(curl -f -s -S -k -d "username=$USERNAME&password=$PASSWORD" "https://$PROXY:8006/api2/json/access/ticket")"
2014-04-03 06:58:08 +02:00
echo "AUTH OK"
2014-03-03 07:22:56 +01:00
2017-09-25 10:56:01 +02:00
TICKET="${DATA//\"/}"
TICKET="${TICKET##*ticket:}"
TICKET="${TICKET%%,*}"
2017-09-25 10:56:02 +02:00
TICKET="${TICKET%%\}*}"
2014-03-03 07:22:56 +01:00
2017-09-25 10:56:01 +02:00
CSRF="${DATA//\"/}"
CSRF="${CSRF##*CSRFPreventionToken:}"
CSRF="${CSRF%%,*}"
2017-09-25 10:56:02 +02:00
CSRF="${CSRF%%\}*}"
2014-03-03 07:22:56 +01:00
2017-09-25 10:56:01 +02: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 07:22:56 +01:00
2017-09-25 10:56:01 +02:00
exec remote-viewer spiceproxy