Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Multi-SSH Connection Script with Alacritty Support
- # Usage: ./multi-ssh.sh [config_file]
- # If no config file is provided, it will use servers.conf in the same directory
- # CONFIG FILE FORMAT.
- # TITLE:USER@HOSTNAME:PORT:PASSWORD
- # EXAMPLE : MyVps:[email protected]:22:passwd12
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- CONFIG_FILE="${1:-$SCRIPT_DIR/servers.conf}"
- # Check if config file exists
- if [[ ! -f "$CONFIG_FILE" ]]; then
- echo "Error: Config file '$CONFIG_FILE' not found!"
- echo "Please create a config file with server details."
- echo "Example format:"
- echo "server1:user@hostname1"
- echo "server2:user@hostname2:port"
- echo "server3:user@hostname3"
- exit 1
- fi
- # Detect terminal emulator
- detect_terminal() {
- if [[ -n "$GNOME_TERMINAL_SCREEN" ]] || command -v gnome-terminal >/dev/null 2>&1; then
- echo "gnome-terminal"
- elif command -v terminator >/dev/null 2>&1; then
- echo "terminator"
- elif command -v konsole >/dev/null 2>&1; then
- echo "konsole"
- elif command -v xfce4-terminal >/dev/null 2>&1; then
- echo "xfce4-terminal"
- elif command -v tilix >/dev/null 2>&1; then
- echo "tilix"
- elif command -v alacritty >/dev/null 2>&1; then
- echo "alacritty"
- else
- echo "unknown"
- fi
- }
- # Function to open SSH connection in new tab/window
- open_ssh_connection() {
- local server_name="$1"
- local ssh_command="$2"
- local terminal="$3"
- case "$terminal" in
- "gnome-terminal")
- gnome-terminal --tab --title="$server_name" -- bash -c "$ssh_command; exec bash"
- ;;
- "terminator")
- terminator --new-tab --title="$server_name" -e "bash -c '$ssh_command; exec bash'"
- ;;
- "konsole")
- konsole --new-tab --title "$server_name" -e bash -c "$ssh_command; exec bash"
- ;;
- "xfce4-terminal")
- xfce4-terminal --tab --title="$server_name" -e "bash -c '$ssh_command; exec bash'"
- ;;
- "tilix")
- tilix --new-session --title="$server_name" -e "bash -c '$ssh_command; exec bash'"
- ;;
- "alacritty")
- alacritty -t "$server_name" -e bash -c "$ssh_command; exec bash" &
- ;;
- *)
- echo "Opening $server_name in new terminal window..."
- x-terminal-emulator -e bash -c "$ssh_command; exec bash" &
- ;;
- esac
- }
- # Main execution
- main() {
- local terminal=$(detect_terminal)
- echo "Detected terminal: $terminal"
- echo "Reading servers from: $CONFIG_FILE"
- echo "Opening SSH connections..."
- if [[ "$terminal" == "unknown" ]]; then
- echo "Error: No supported terminal emulator found!"
- exit 1
- fi
- while IFS=':' read -r server_name ssh_target port password || [[ -n "$server_name" ]]; do
- # Skip empty lines and comments
- [[ -z "$server_name" || "$server_name" =~ ^[[:space:]]*# ]] && continue
- # Default port
- [[ -z "$port" ]] && port=22
- # Build SSH command with sshpass
- ssh_cmd="sshpass -p '$password' ssh -t -o StrictHostKeyChecking=no -p $port $ssh_target 'export TERM=xterm; htop; exec bash'"
- echo "Connecting to $server_name ($ssh_target)..."
- open_ssh_connection "$server_name" "$ssh_cmd" "$terminal"
- sleep 0.5
- done < "$CONFIG_FILE"
- echo "All SSH connections initiated!"
- }
- # Run the script
- main
Advertisement