tmux-management 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env bash
  2. # This script launches a multi-pane tmux session for multiple remotes
  3. # TODO make remote sessions run in tmux too
  4. # Copyright 2024 Bryan C. Roessler
  5. # Use DEBUG=1 ./tmux-management to enable debugging
  6. DEBUG=${DEBUG:-0}
  7. debug() { (( DEBUG )) && echo "Debug: $*"; }
  8. HOSTS=(
  9. workstation
  10. laptop
  11. #hartmanlab
  12. )
  13. debug "Hosts list: ${HOSTS[*]}"
  14. # Remove localhost from HOSTS list
  15. TARGETS=()
  16. for HOST in "${HOSTS[@]}"; do
  17. if [[ "$HOST" != "$HOSTNAME" ]]; then
  18. TARGETS+=("$HOST")
  19. fi
  20. done
  21. unset HOST
  22. # Create a unique session name from the remaining hosts
  23. SESSION=$(IFS=- ; echo "${TARGETS[*]}")
  24. debug "Targets: ${TARGETS[*]}"
  25. debug "Session: $SESSION"
  26. # Connect to existing session
  27. if tmux has-session -t "$SESSION" &> /dev/null; then
  28. tmux attach -t "$SESSION";
  29. else # create new tmux session
  30. tmux new-session -d -s "$SESSION" -n "$SESSION";
  31. # Create a new pane for each TARGET
  32. for TARGET in "${TARGETS[@]}"; do
  33. tmux split-window -h;
  34. tmux send "ssh $TARGET" enter;
  35. done
  36. tmux set-window-option synchronize-panes on;
  37. tmux select-pane -t "$SESSION:0.0"
  38. tmux send 'clear' enter;
  39. tmux attach-session -t "$SESSION"
  40. fi
  41. unset TARGET