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