proxmox-api-bash/vm_control.sh

56 lines
1.1 KiB
Bash
Executable File

#!/bin/sh
USERNAME="$1"
PASSWORD="$2"
REALM="$3"
TARGET_VMID="$4"
HOST="https://172.23.92.239:8006"
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
(
OUTPUT=$($CURL -s -k -m 10 -d "username=$USERNAME@$REALM&password=$PASSWORD" "$HOST/api2/json/access/ticket")
TICKET=$(echo "$OUTPUT" | jq -r '.data | .ticket')
CSRF=$(echo "$OUTPUT" | jq -r '.data | .CSRFPreventionToken')
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)
echo "$START_TASK_DATA" | $JQ
) &
exit 0