#!/bin/bash # JetKVM helper — authenticate and interact with JetKVM device. # Usage: # jetkvm.sh status — check device status # jetkvm.sh reboot — reboot the target machine via ATX # jetkvm.sh poweron — power on via ATX short press # jetkvm.sh poweroff — power off via ATX long press # # Environment: # JETKVM_HOST — JetKVM IP (default: 192.168.3.10) # JETKVM_PASS — device password set -euo pipefail HOST="${JETKVM_HOST:-192.168.3.10}" PASS="${JETKVM_PASS:-}" if [ -z "$PASS" ]; then echo "ERROR: JETKVM_PASS not set" >&2 exit 1 fi BASE="http://$HOST" # Authenticate and get token login() { local resp resp=$(curl -s -X POST "$BASE/auth/login-local" \ -H "Content-Type: application/json" \ -d "{\"password\":\"$PASS\"}" 2>&1) local token token=$(echo "$resp" | grep -oP '"token"\s*:\s*"[^"]*"' | head -1 | grep -oP '"[^"]*"$' | tr -d '"') if [ -z "$token" ]; then echo "ERROR: Login failed: $resp" >&2 exit 1 fi echo "$token" } # Make authenticated request api() { local method="$1" path="$2" body="${3:-}" local token token=$(login) if [ -n "$body" ]; then curl -s -X "$method" "$BASE$path" \ -H "Authorization: Bearer $token" \ -H "Content-Type: application/json" \ -d "$body" else curl -s -X "$method" "$BASE$path" \ -H "Authorization: Bearer $token" fi } case "${1:-status}" in status) curl -s "$BASE/device/status" 2>&1 ;; device) api GET /device ;; reboot) echo "Sending ATX reset..." api POST /device/atx/reset ;; poweron) echo "Sending ATX short power press..." api POST /device/atx/power-short ;; poweroff) echo "Sending ATX long power press..." api POST /device/atx/power-long ;; *) echo "Usage: $0 {status|device|reboot|poweron|poweroff}" exit 1 ;; esac