#!/bin/bash # Script to register an SSH server with MiniDiscovery service # Default values BASE_URL="http://localhost:8500" SERVER_NAME="ssh-server" SERVER_ADDRESS="127.0.0.1" SERVER_PORT=22 TAGS="ssh,secure" # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -t|--token) API_TOKEN="$2" shift 2 ;; -u|--url) BASE_URL="$2" shift 2 ;; -n|--name) SERVER_NAME="$2" shift 2 ;; -a|--address) SERVER_ADDRESS="$2" shift 2 ;; -p|--port) SERVER_PORT="$2" shift 2 ;; --tags) TAGS="$2" shift 2 ;; -h|--help) echo "Usage: $0 -t API_TOKEN [options]" echo "Options:" echo " -t, --token TOKEN API token for authentication (required)" echo " -u, --url URL Base URL of the API (default: $BASE_URL)" echo " -n, --name NAME Server name (default: $SERVER_NAME)" echo " -a, --address ADDRESS Server address (default: $SERVER_ADDRESS)" echo " -p, --port PORT SSH port (default: $SERVER_PORT)" echo " --tags TAGS Comma-separated tags (default: $TAGS)" echo " -h, --help Show this help message" exit 0 ;; *) echo "Unknown option: $1" exit 1 ;; esac done # Check if API token is provided if [ -z "$API_TOKEN" ]; then echo "Error: API token is required" echo "Use -t or --token to provide the API token" exit 1 fi # Generate a unique service ID SERVICE_ID="${SERVER_NAME}-$(uuidgen || cat /proc/sys/kernel/random/uuid)" # Convert comma-separated tags to JSON array IFS=',' read -ra TAG_ARRAY <<< "$TAGS" TAGS_JSON=$(printf '"%s",' "${TAG_ARRAY[@]}" | sed 's/,$//') TAGS_JSON="[$TAGS_JSON]" # Prepare JSON payload JSON_PAYLOAD=$(cat < .last_registered_service_id else echo "Registration failed." echo "Response: $RESPONSE" exit 1 fi