diff --git a/scripts/tmux_ssh_group.py b/scripts/tmux_ssh_group.py index 81b9f45..c139555 100644 --- a/scripts/tmux_ssh_group.py +++ b/scripts/tmux_ssh_group.py @@ -2,33 +2,55 @@ import libtmux import configparser import time import sys +import os from pathlib import Path from pyfzf.pyfzf import FzfPrompt -CONF_KEYS = [] +BASE_PATH = Path("~/.ssh-tmux/").expanduser() + +configs = {} +sections = {} +for subconffile in os.listdir(BASE_PATH): + config = configparser.ConfigParser() + config.read(BASE_PATH / subconffile) + + configs[subconffile] = config + + for section in config.sections(): + sections[section] = subconffile -config = configparser.ConfigParser() -config.read(Path("~/.ssh-tmux-groups.ini").expanduser()) fzf = FzfPrompt() -group = fzf.prompt(config.sections(), "--cycle")[0] +selected_group = fzf.prompt(sections.keys(), "--cycle")[0] -servers = [] -for key, enabled in config[group].items(): - if key in CONF_KEYS: - continue +config = configs[sections[selected_group]] - if enabled == "true": - servers.append(key) +servers = config[selected_group]["servers"].split("\n") + +extra_commands = [] +if "commands" in config[selected_group].keys(): + extra_commands = config[selected_group]["commands"].split("\n") if not servers: sys.exit(1) srv = libtmux.Server() -active_session = srv.sessions.filter(session_attached='1')[0] +active_sessions = srv.sessions.filter(session_attached='1') + +if active_sessions: + active_session = active_sessions[0] + +else: + raw_try = srv.cmd("display", "-p", "#{session_name}").stdout + if raw_try: + active_session = srv.sessions.filter(name=raw_try[0])[0] + + else: + active_session = srv.sessions[0] + window = active_session.new_window(f"ssh-multig {','.join(servers)}", window_shell=f"ssh {servers[0]}") for server in servers[1:]: @@ -48,6 +70,10 @@ for pane in window.panes: window.set_window_option("synchronize-panes", "on") pane = window.panes[0] pane.send_keys("sudo su -") + +for command in extra_commands: + pane.send_keys(command) + window.set_window_option("synchronize-panes", "off") window.select() diff --git a/tmux.conf b/tmux.conf index bfae596..e59f0d1 100644 --- a/tmux.conf +++ b/tmux.conf @@ -67,4 +67,4 @@ bind-key -r f run-shell "tmux neww ~/Documents/dotfiles/scripts/tmux-sessionizer bind-key -r m run-shell "tmux neww ~/Documents/dotfiles/scripts/tmux-multi-ssh" bind-key -r m run-shell "tmux neww /home/legrems/miniconda3/bin/python ~/Documents/dotfiles/scripts/tmux_ssh_group.py" bind-key -r j run-shell "tmux neww /home/legrems/miniconda3/bin/python ~/Documents/dotfiles/scripts/tmux_multi_ssh.py" -bind-key -r i set-window-option synchronize-panes\; display-message "synchronize-panes is now #[bg={?pane_synchronized, red, blue}]#{?pane_synchronized,!! ON !!,OFF}" +bind-key -r i set-window-option synchronize-panes\; display-message "synchronize-panes is now #{?pane_synchronized, #[bg=red]!! ON !!,OFF}"