homelab infrastructure services
1#!/bin/bash
2# tin key test - Test SSH connection to NAS server
3
4set -euo pipefail
5
6# Get tinsnip root and source libraries
7SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8TINSNIP_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")"
9source "$TINSNIP_ROOT/lib/core.sh"
10source "$TINSNIP_ROOT/lib/registry.sh"
11
12show_help() {
13 cat << EOF
14tin key test - Test SSH connection to NAS server
15
16USAGE:
17 tin key test <nas-server> [username]
18
19EXAMPLES:
20 tin key test DS412plus.local # Test connection with current user
21 tin key test 192.168.0.206 [username] # Test connection with specific user
22
23DESCRIPTION:
24 Tests SSH connectivity to a NAS server using the managed SSH key.
25 Verifies that the key is properly installed and working.
26
27 Username defaults to current user if not specified.
28EOF
29}
30
31# Test SSH connection
32test_connection() {
33 local nas_server="$1"
34 local nas_user="${2:-$(whoami)}"
35
36 # Check if station is available
37 if ! check_tinsnip_station 2>/dev/null; then
38 error_with_prefix "Key Test" "topsheet.station-prod not found"
39 echo "Set up topsheet first: TIN_SHEET=topsheet tin machine station prod <nas-server>" >&2
40 exit 1
41 fi
42
43 log_with_prefix "Key Test" "Testing SSH connection to: $nas_server"
44 echo
45
46 local keys_dir="$NAS_CREDENTIALS_DIR/ssh-keys"
47 local private_key="$keys_dir/$nas_server.key"
48
49 if [[ ! -f "$private_key" ]]; then
50 echo "No SSH key found for $nas_server"
51 echo "Generate with: tin key generate $nas_server"
52 exit 1
53 fi
54
55 echo "Testing SSH connection..."
56 echo "Key file: $private_key"
57 echo
58
59 # Test SSH connection with timeout
60 if timeout 10 ssh -i "$private_key" -o ConnectTimeout=5 -o StrictHostKeyChecking=no -o BatchMode=yes "${nas_user}@$nas_server" "echo 'Connection successful'" 2>/dev/null; then
61 echo "✅ SSH connection successful"
62 echo "Key is working correctly"
63 else
64 echo "❌ SSH connection failed"
65 echo
66 echo "Possible issues:"
67 echo "1. Key not installed on NAS server"
68 echo " Try: tin key install $nas_server"
69 echo "2. Network connectivity issues"
70 echo "3. SSH service not running on NAS"
71 echo "4. Key permissions or format issues"
72 exit 1
73 fi
74}
75
76# Main command handler
77main() {
78 case "${1:-}" in
79 help|--help|-h)
80 show_help
81 ;;
82 "")
83 echo "Error: NAS server required" >&2
84 echo "Usage: tin key test <nas-server> [username]" >&2
85 exit 1
86 ;;
87 *)
88 test_connection "$1" "${2:-}"
89 ;;
90 esac
91}
92
93main "$@"