forked from ProjoMania/OdooDeveloperTools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnectSSHServer
More file actions
executable file
·70 lines (55 loc) · 1.87 KB
/
ConnectSSHServer
File metadata and controls
executable file
·70 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# === Settings ===
CONFIG_DIR="$HOME/.ssh/config.d"
if [[ ! -d "$CONFIG_DIR" ]]; then
echo "❌ No config.d directory found at $CONFIG_DIR"
exit 1
fi
# === Load servers ===
SERVER_LIST=()
HOST_LIST=()
for file in "$CONFIG_DIR"/*.conf; do
if [[ -f "$file" ]]; then
# Initialize variables
SERVER_NAME=""
IP_ADDRESS=""
SSH_USER=""
# Read file line by line
while IFS= read -r line; do
# Trim leading whitespace for pattern matching
trimmed_line=$(echo "$line" | sed 's/^[[:space:]]*//')
if [[ "$trimmed_line" =~ ^Host[[:space:]]+(.+) ]]; then
SERVER_NAME="${BASH_REMATCH[1]}"
elif [[ "$trimmed_line" =~ ^HostName[[:space:]]+(.+) ]]; then
IP_ADDRESS="${BASH_REMATCH[1]}"
elif [[ "$trimmed_line" =~ ^User[[:space:]]+(.+) ]]; then
SSH_USER="${BASH_REMATCH[1]}"
fi
done < "$file"
if [[ -n "$SERVER_NAME" && -n "$IP_ADDRESS" ]]; then
SERVER_LIST+=("$SERVER_NAME - $IP_ADDRESS as $SSH_USER")
HOST_LIST+=("$SERVER_NAME")
fi
fi
done
# === No servers found ===
if [[ ${#SERVER_LIST[@]} -eq 0 ]]; then
echo "❌ No servers found in $CONFIG_DIR"
exit 1
fi
# === Display Menu ===
echo "=============================="
echo " 🖥️ Quick SSH Connect"
echo "=============================="
for i in "${!SERVER_LIST[@]}"; do
printf "%3d) %s\n" "$((i+1))" "${SERVER_LIST[$i]}"
done
echo "=============================="
read -p "Enter number to connect: " choice
if [[ ! "$choice" =~ ^[0-9]+$ ]] || [[ "$choice" -lt 1 ]] || [[ "$choice" -gt "${#HOST_LIST[@]}" ]]; then
echo "❌ Invalid choice!"
exit 1
fi
SELECTED_SERVER="${HOST_LIST[$((choice-1))]}"
echo "🔌 Connecting to $SELECTED_SERVER ..."
ssh "$SELECTED_SERVER"