· 3 months ago · Jul 07, 2025, 12:25 PM
1#!/bin/bash
2
3# Multi-SSH Connection Script with Alacritty Support
4# Usage: ./multi-ssh.sh [config_file]
5# If no config file is provided, it will use servers.conf in the same directory
6# CONFIG FILE FORMAT.
7# TITLE:USER@HOSTNAME:PORT:PASSWORD
8# EXAMPLE : MyVps:ubuntu@127.0.0.1:22:passwd12
9
10SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11CONFIG_FILE="${1:-$SCRIPT_DIR/servers.conf}"
12
13# Check if config file exists
14if [[ ! -f "$CONFIG_FILE" ]]; then
15 echo "Error: Config file '$CONFIG_FILE' not found!"
16 echo "Please create a config file with server details."
17 echo "Example format:"
18 echo "server1:user@hostname1"
19 echo "server2:user@hostname2:port"
20 echo "server3:user@hostname3"
21 exit 1
22fi
23
24# Detect terminal emulator
25detect_terminal() {
26 if [[ -n "$GNOME_TERMINAL_SCREEN" ]] || command -v gnome-terminal >/dev/null 2>&1; then
27 echo "gnome-terminal"
28 elif command -v terminator >/dev/null 2>&1; then
29 echo "terminator"
30 elif command -v konsole >/dev/null 2>&1; then
31 echo "konsole"
32 elif command -v xfce4-terminal >/dev/null 2>&1; then
33 echo "xfce4-terminal"
34 elif command -v tilix >/dev/null 2>&1; then
35 echo "tilix"
36 elif command -v alacritty >/dev/null 2>&1; then
37 echo "alacritty"
38 else
39 echo "unknown"
40 fi
41}
42
43# Function to open SSH connection in new tab/window
44open_ssh_connection() {
45 local server_name="$1"
46 local ssh_command="$2"
47 local terminal="$3"
48
49 case "$terminal" in
50 "gnome-terminal")
51 gnome-terminal --tab --title="$server_name" -- bash -c "$ssh_command; exec bash"
52 ;;
53 "terminator")
54 terminator --new-tab --title="$server_name" -e "bash -c '$ssh_command; exec bash'"
55 ;;
56 "konsole")
57 konsole --new-tab --title "$server_name" -e bash -c "$ssh_command; exec bash"
58 ;;
59 "xfce4-terminal")
60 xfce4-terminal --tab --title="$server_name" -e "bash -c '$ssh_command; exec bash'"
61 ;;
62 "tilix")
63 tilix --new-session --title="$server_name" -e "bash -c '$ssh_command; exec bash'"
64 ;;
65 "alacritty")
66 alacritty -t "$server_name" -e bash -c "$ssh_command; exec bash" &
67 ;;
68 *)
69 echo "Opening $server_name in new terminal window..."
70 x-terminal-emulator -e bash -c "$ssh_command; exec bash" &
71 ;;
72 esac
73}
74
75# Main execution
76main() {
77 local terminal=$(detect_terminal)
78 echo "Detected terminal: $terminal"
79 echo "Reading servers from: $CONFIG_FILE"
80 echo "Opening SSH connections..."
81
82 if [[ "$terminal" == "unknown" ]]; then
83 echo "Error: No supported terminal emulator found!"
84 exit 1
85 fi
86
87 while IFS=':' read -r server_name ssh_target port password || [[ -n "$server_name" ]]; do
88 # Skip empty lines and comments
89 [[ -z "$server_name" || "$server_name" =~ ^[[:space:]]*# ]] && continue
90
91 # Default port
92 [[ -z "$port" ]] && port=22
93
94 # Build SSH command with sshpass
95 ssh_cmd="sshpass -p '$password' ssh -t -o StrictHostKeyChecking=no -p $port $ssh_target 'export TERM=xterm; htop; exec bash'"
96
97 echo "Connecting to $server_name ($ssh_target)..."
98 open_ssh_connection "$server_name" "$ssh_cmd" "$terminal"
99
100 sleep 0.5
101 done < "$CONFIG_FILE"
102
103 echo "All SSH connections initiated!"
104}
105
106# Run the script
107main
108