scripts/ssh-manager.sh

73 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
SESSION="cluster"
CONFIG_FILE="$HOME/.ssh/tmux-hosts.conf"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file not found: $CONFIG_FILE"
exit 1
fi
# Kill existing session
tmux kill-session -t $SESSION 2>/dev/null
# Create new session
tmux new-session -d -s $SESSION -n "Control"
tmux set -t $SESSION -g mouse on
tmux set -t $SESSION -g pane-border-status top
# Count hosts
host_count=0
while IFS=: read -r name connection port description; do
[[ -z "$name" || "$name" =~ ^# ]] && continue
((host_count++))
done < "$CONFIG_FILE"
multiview_window=$((host_count + 1))
# Create individual host windows
window_num=1
hosts=()
while IFS=: read -r name connection port description; do
[[ -z "$name" || "$name" =~ ^# ]] && continue
port=${port:-22}
hosts+=("$name:$connection:$port:$description")
tmux new-window -t $SESSION:$window_num -n "$name"
tmux select-pane -t $SESSION:$window_num.0 -T "$description"
if [ "$port" != "22" ]; then
tmux send-keys -t $SESSION:$window_num "ssh -p $port $connection" C-m
else
tmux send-keys -t $SESSION:$window_num "ssh $connection" C-m
fi
((window_num++))
done < "$CONFIG_FILE"
# Create multi-view window
if [ ${#hosts[@]} -gt 0 ]; then
tmux new-window -t $SESSION:$multiview_window -n "Multi-View"
num_hosts=${#hosts[@]}
for ((i=1; i<num_hosts; i++)); do
tmux split-window -t $SESSION:$multiview_window
tmux select-layout -t $SESSION:$multiview_window tiled
done
pane_num=0
for host_info in "${hosts[@]}"; do
IFS=: read -r name connection port description <<< "$host_info"
tmux select-pane -t $SESSION:$multiview_window.$pane_num -T "$name"
if [ "$port" != "22" ]; then
tmux send-keys -t $SESSION:$multiview_window.$pane_num "ssh -p $port $connection" C-m
else
tmux send-keys -t $SESSION:$multiview_window.$pane_num "ssh $connection" C-m
fi
((pane_num++))
done
fi
# Setup control window with menu hint
tmux send-keys -t $SESSION:0 "echo '=== Cluster Control ===' && echo 'Hosts: ${#hosts[@]}' && echo 'Press Ctrl+b then window number to switch' && echo 'Window $multiview_window = Multi-View (all hosts)' && echo '' && echo 'Run ~/scripts/control-menu.sh for Ansible actions' && echo ''" C-m
tmux select-window -t $SESSION:0
tmux attach -t $SESSION