proxmox-api-bash/vm_control.sh

56 lines
1.1 KiB
Bash
Raw Normal View History

2019-08-31 21:41:33 +02:00
#!/bin/sh
USERNAME="$1"
PASSWORD="$2"
REALM="$3"
TARGET_VMID="$4"
2019-09-02 21:13:42 +02:00
HOST="https://172.23.92.239:8006"
2019-08-31 21:41:33 +02:00
NODE="proxmox"
JQ=$(which jq)
if [ -z "$JQ" ]; then
echo "Please install jq!"; exit 0
fi
CURL=$(which curl)
if [ -z "$CURL" ]; then
echo "Please install curl!"; exit 0
fi
if [ "$5" = "current" ]; then
TARGET_ACTION="$5"
elif [ "$5" = "reset" ]; then
TARGET_ACTION="$5"
elif [ "$5" = "resume" ]; then
TARGET_ACTION="$5"
elif [ "$5" = "shutdown" ]; then
TARGET_ACTION="$5"
elif [ "$5" = "start" ]; then
TARGET_ACTION="$5"
elif [ "$5" = "stop" ]; then
TARGET_ACTION="$5"
elif [ "$5" = "suspend" ]; then
TARGET_ACTION="$5"
fi
2019-09-06 22:51:27 +02:00
(
OUTPUT=$($CURL -s -k -m 10 -d "username=$USERNAME@$REALM&password=$PASSWORD" "$HOST/api2/json/access/ticket")
2019-08-31 21:41:33 +02:00
2019-09-06 22:51:27 +02:00
TICKET=$(echo "$OUTPUT" | jq -r '.data | .ticket')
CSRF=$(echo "$OUTPUT" | jq -r '.data | .CSRFPreventionToken')
2019-08-31 21:41:33 +02:00
2019-09-06 22:51:27 +02:00
START_TASK_DATA=$($CURL -s -k -m 10 -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" -X POST $HOST/api2/json/nodes/$NODE/qemu/$TARGET_VMID/status/$TARGET_ACTION)
2019-08-31 21:41:33 +02:00
2019-09-06 22:51:27 +02:00
echo "$START_TASK_DATA" | $JQ
) &
2019-08-31 21:41:33 +02:00
2019-09-06 22:51:27 +02:00
exit 0