Wednesday, November 16, 2022

Tmux : Create multiple windows to ssh to several servers

 #!/usr/local/bin/zsh

SESSION="Maintenance-X"

tmux new -s "${SESSION}" -d
tmux rename-window -t "${SESSION}.0" Main
tmux new-window -d -t "${SESSION}" -n Server1
tmux new-window -d -t "${SESSION}" -n Server2
tmux new-window -d -t "${SESSION}" -n Server3

# Run ssh command in each window
tmux send-keys -t "${SESSION}:1" 'ssh server1.mydomain' Enter
tmux send-keys -t "${SESSION}:2" 'ssh server2.mydomain' Enter
tmux send-keys -t "${SESSION}:3" 'ssh server3.mydomain' Enter

# To run command in a pane within a window, use:
# tmux send-keys -t "${SESSION}:1.<pane-position>" 'ssh server1' Enter
# tmux send-keys -t "${SESSION}:1.top" 'ssh server1' Enter


tmux attach-session -d -t "$SESSION"