Update tmux-ssh-groups to use extra commands and multiple config files

This commit is contained in:
Loïc Gremaud 2024-04-10 02:56:08 +02:00
parent 4db1976bcd
commit 8533f6a3ab
2 changed files with 38 additions and 12 deletions

View File

@ -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(Path("~/.ssh-tmux-groups.ini").expanduser())
config.read(BASE_PATH / subconffile)
configs[subconffile] = config
for section in config.sections():
sections[section] = subconffile
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()

View File

@ -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}"