91 lines
3.3 KiB
Bash
Executable file
91 lines
3.3 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Configuration
|
|
API_URL="http://localhost:8000/api/devices"
|
|
|
|
# Create a logs directory for output files
|
|
LOGS_DIR="./device_logs"
|
|
mkdir -p $LOGS_DIR
|
|
|
|
# Function to check if a command exists
|
|
command_exists() {
|
|
command -v "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
# Check if curl or wget is available
|
|
if command_exists curl; then
|
|
echo "Using curl to fetch data..."
|
|
RESPONSE=$(curl -s $API_URL)
|
|
elif command_exists wget; then
|
|
echo "Using wget to fetch data..."
|
|
RESPONSE=$(wget -qO- $API_URL)
|
|
else
|
|
echo "Error: Neither curl nor wget is available. Please install one of them."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if response is empty
|
|
if [ -z "$RESPONSE" ]; then
|
|
echo "Error: Received empty response from $API_URL"
|
|
echo "Make sure the API server is running and the /api/devices endpoint exists."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if jq is available for JSON parsing
|
|
if command_exists jq; then
|
|
echo "Using jq to parse JSON..."
|
|
|
|
# Extract devices using jq
|
|
# This is flexible to handle different JSON structures
|
|
|
|
# Try to parse top-level array first
|
|
DEVICE_COUNT=$(echo $RESPONSE | jq '. | length' 2>/dev/null)
|
|
|
|
# If not an array or empty, try .devices field (common pattern)
|
|
if [ -z "$DEVICE_COUNT" ] || [ "$DEVICE_COUNT" = "0" ]; then
|
|
DEVICE_COUNT=$(echo $RESPONSE | jq '.devices | length' 2>/dev/null)
|
|
|
|
if [ -n "$DEVICE_COUNT" ] && [ "$DEVICE_COUNT" -gt 0 ]; then
|
|
echo "Found $DEVICE_COUNT devices in .devices field"
|
|
echo "-------------------------------------"
|
|
|
|
for i in $(seq 0 $(($DEVICE_COUNT-1))); do
|
|
NAME=$(echo $RESPONSE | jq -r ".devices[$i].name // .devices[$i].hostname // .devices[$i].device // \"Unknown\"")
|
|
IP=$(echo $RESPONSE | jq -r ".devices[$i].ip // .devices[$i].ipaddress // .devices[$i].ip_address // \"Unknown\"")
|
|
TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
echo "Connecting to $NAME ($IP) at $TIME"
|
|
ssh user@$IP "get vpn ssl monitor" > "$LOGS_DIR/${NAME}_${TIME}.log"
|
|
done
|
|
else
|
|
echo "No devices found or unexpected JSON structure."
|
|
echo "Raw API response:"
|
|
echo "$RESPONSE"
|
|
fi
|
|
else
|
|
echo "Found $DEVICE_COUNT devices in top-level array"
|
|
echo "-------------------------------------"
|
|
|
|
for i in $(seq 0 $(($DEVICE_COUNT-1))); do
|
|
NAME=$(echo $RESPONSE | jq -r ".[$i].name // .[$i].hostname // .[$i].device // \"Unknown\"")
|
|
IP=$(echo $RESPONSE | jq -r ".[$i].ip // .[$i].ipaddress // .[$i].ip_address // \"Unknown\"")
|
|
TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
echo "Connecting to $NAME ($IP) at $TIME"
|
|
ssh user@$IP "get vpn ssl monitor" > "$LOGS_DIR/${NAME}_${TIME}.log"
|
|
done
|
|
fi
|
|
else
|
|
echo "Warning: jq is not available for proper JSON parsing. Using basic parsing..."
|
|
|
|
# Very basic parsing fallback without jq (limited)
|
|
# Extract name and IP with grep and sed - this is fragile but better than nothing
|
|
echo "$RESPONSE" | grep -o '"name"\s*:\s*"[^"]*"\|"ip"\s*:\s*"[^"]*"' | sed 's/"name"\s*:\s*"\([^"]*\)"/\1/g; s/"ip"\s*:\s*"\([^"]*\)"/\1/g' | paste -d " " - - | while read -r NAME IP; do
|
|
TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
echo "Connecting to $NAME ($IP) at $TIME"
|
|
ssh user@$IP "get vpn ssl monitor" > "$LOGS_DIR/${NAME}_${TIME}.log"
|
|
done
|
|
fi
|
|
|
|
echo "-------------------------------------"
|
|
echo "Script completed. Log files saved to $LOGS_DIR directory."
|