Compare commits
No commits in common. "953b9a791c8ef9f2bf62253ce18dccd837519be0" and "fd029a8ce24e70d413639876e4e50653d1af1bf2" have entirely different histories.
953b9a791c
...
fd029a8ce2
20
README.md
20
README.md
@ -1,20 +0,0 @@
|
|||||||
# Scripts
|
|
||||||
|
|
||||||
## tmux-sessionizer
|
|
||||||
Inspired by [ThePrimeagen](https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/scripts/tmux-sessionizer), but in python
|
|
||||||
|
|
||||||
Create/load tmux session based on the path of a project, use fzf for selection
|
|
||||||
|
|
||||||
## tmux-ssh-group
|
|
||||||
Open multiple ssh connection to multiple servers as sudo, can pass extra commands also.
|
|
||||||
Will open a new window in tiled mode on all this servers at the same times
|
|
||||||
|
|
||||||
Read from config files located under `~/.ssh-tmux/`, with the format:
|
|
||||||
```
|
|
||||||
[Gestion controller]
|
|
||||||
servers = myserver1.fqdn
|
|
||||||
myserver2.fqdn
|
|
||||||
myserver3.fqdn
|
|
||||||
myserver4.fqdn
|
|
||||||
commands = ls -l
|
|
||||||
```
|
|
||||||
14
tmux-cht.sh
Executable file
14
tmux-cht.sh
Executable file
@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
selected=`cat ~/.tmux-cht-languages ~/.tmux-cht-command | fzf`
|
||||||
|
if [[ -z $selected ]]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
read -p "Enter Query: " query
|
||||||
|
|
||||||
|
if grep -qs "$selected" ~/.tmux-cht-languages; then
|
||||||
|
query=`echo $query | tr ' ' '+'`
|
||||||
|
tmux neww bash -c "echo \"curl cht.sh/$selected/$query/\" & curl cht.sh/$selected/$query & while [ : ]; do sleep 1; done"
|
||||||
|
else
|
||||||
|
tmux neww bash -c "curl -s cht.sh/$selected~$query | less"
|
||||||
|
fi
|
||||||
26
tmux-sessionizer
Executable file
26
tmux-sessionizer
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
if [[ $# -eq 1 ]]; then
|
||||||
|
selected=$1
|
||||||
|
else
|
||||||
|
selected=$(find ~/Documents/Arcanite/ ~/Documents/PolyLAN/ ~/Documents/Python ~/Documents/ -mindepth 1 -maxdepth 1 -type d | fzf)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z $selected ]]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
selected_name=$(basename "$selected" | tr . _)
|
||||||
|
tmux_running=$(pgrep tmux)
|
||||||
|
|
||||||
|
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
|
||||||
|
tmux new-session -s $selected_name -c $selected
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! tmux has-session -t=$selected_name 2> /dev/null; then
|
||||||
|
tmux new-session -ds $selected_name -c $selected
|
||||||
|
fi
|
||||||
|
|
||||||
|
tmux switch-client -t $selected_name
|
||||||
|
|
||||||
@ -2,29 +2,17 @@ import configparser
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import libtmux
|
import libtmux
|
||||||
import argparse
|
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pyfzf.pyfzf import FzfPrompt
|
from pyfzf.pyfzf import FzfPrompt
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument("--servers-file", "-f", help="Servers file to connect to")
|
|
||||||
parser_args = parser.parse_args()
|
|
||||||
|
|
||||||
if parser_args.servers_file:
|
|
||||||
with open(parser_args.servers_file, "r") as file:
|
|
||||||
data = file.read()
|
|
||||||
|
|
||||||
servers = []
|
|
||||||
for line in data.strip().split("\n"):
|
|
||||||
servers.append(line.strip())
|
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config_path = Path("~/.ssh-tmux-multi.ini").expanduser()
|
config_path = Path("~/.ssh-tmux-multi.ini").expanduser()
|
||||||
config.read(config_path)
|
config.read(config_path)
|
||||||
|
|
||||||
fav_key = "Favourite servers"
|
fav_key = "Favourite servers"
|
||||||
|
|
||||||
fzf = FzfPrompt()
|
fzf = FzfPrompt()
|
||||||
|
|
||||||
|
|
||||||
@ -85,8 +73,7 @@ def write_choosen_servers(servers):
|
|||||||
with open(config_path, "w") as file:
|
with open(config_path, "w") as file:
|
||||||
config.write(file)
|
config.write(file)
|
||||||
|
|
||||||
if not parser_args.servers_file:
|
servers = fzf.prompt(get_favourite_servers_first(), "--cycle --multi --print-query")
|
||||||
servers = fzf.prompt(["Select server to connect to"] + get_favourite_servers_first(), "--cycle --multi --print-query --header-lines 1")
|
|
||||||
|
|
||||||
# Strip from query if found else, use the query
|
# Strip from query if found else, use the query
|
||||||
if len(servers) > 1:
|
if len(servers) > 1:
|
||||||
|
|||||||
@ -1,20 +0,0 @@
|
|||||||
import libtmux
|
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
from pyfzf.pyfzf import FzfPrompt
|
|
||||||
|
|
||||||
# Get active tmux sessions
|
|
||||||
srv = libtmux.Server()
|
|
||||||
|
|
||||||
fzf = FzfPrompt()
|
|
||||||
selections = fzf.prompt(["Select one session you want to switch to"] + [s.name for s in srv.sessions], "--cycle --header-lines 1")
|
|
||||||
|
|
||||||
if not selections:
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
session_name = selections[0]
|
|
||||||
|
|
||||||
session = srv.sessions.filter(name=session_name)[0]
|
|
||||||
session.switch_client()
|
|
||||||
session.active_window.active_pane.display_message(f"Switched to session: {session_name}")
|
|
||||||
@ -1,30 +0,0 @@
|
|||||||
import libtmux
|
|
||||||
import sh
|
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
from pyfzf.pyfzf import FzfPrompt
|
|
||||||
|
|
||||||
fzf = FzfPrompt()
|
|
||||||
folders = [
|
|
||||||
"~/Documents/Arcanite/",
|
|
||||||
"~/Documents/PolyLAN/",
|
|
||||||
"~/Documents/Python/",
|
|
||||||
"~/Documents/",
|
|
||||||
]
|
|
||||||
|
|
||||||
available_folders = sh.find(*[Path(f).expanduser() for f in folders] + "-mindepth 1 -maxdepth 1 -type d".split(" ")).strip().split("\n")
|
|
||||||
selected = fzf.prompt(["Select a folder to create or switch session to"] + available_folders, "--cycle --header-lines 1")
|
|
||||||
|
|
||||||
if not selected:
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
selected = selected[0]
|
|
||||||
session_name = selected.split("/")[-1]
|
|
||||||
srv = libtmux.Server()
|
|
||||||
|
|
||||||
if not srv.has_session(session_name):
|
|
||||||
srv.new_session(session_name, attach=False, start_directory=selected)
|
|
||||||
|
|
||||||
srv.switch_client(session_name)
|
|
||||||
@ -1,6 +1,5 @@
|
|||||||
import libtmux
|
import libtmux
|
||||||
import configparser
|
import configparser
|
||||||
import argparse
|
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
@ -10,69 +9,41 @@ from pathlib import Path
|
|||||||
from pyfzf.pyfzf import FzfPrompt
|
from pyfzf.pyfzf import FzfPrompt
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument("--no-open", action="store_true", help="Do not open tmux window. Mostly for debugging")
|
|
||||||
parser.add_argument("--separate-sessions", "-s", action="store_true", help="Use a separate sessions for all of the groups")
|
|
||||||
parser_args = parser.parse_args()
|
|
||||||
|
|
||||||
|
|
||||||
def get_window_name(session_name):
|
|
||||||
name = session_name.replace("-", "").replace(" ", " ").replace(" ", "_")
|
|
||||||
return f"{'' if parser_args.separate_sessions else 'ssh-multig '}{name}"
|
|
||||||
|
|
||||||
BASE_PATH = Path("~/.ssh-tmux/").expanduser()
|
BASE_PATH = Path("~/.ssh-tmux/").expanduser()
|
||||||
|
|
||||||
configs = {}
|
configs = {}
|
||||||
sections = {}
|
sections = {}
|
||||||
for subconffile in os.listdir(BASE_PATH):
|
for subconffile in os.listdir(BASE_PATH):
|
||||||
if not subconffile.endswith(".ini"):
|
|
||||||
continue
|
|
||||||
|
|
||||||
prefix = subconffile.replace(".ini", "").capitalize()
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(BASE_PATH / subconffile)
|
config.read(BASE_PATH / subconffile)
|
||||||
|
|
||||||
configs[subconffile] = config
|
configs[subconffile] = config
|
||||||
|
|
||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
section_name = f"[{prefix}] {section}"
|
sections[section] = subconffile
|
||||||
sections[section_name] = {
|
|
||||||
"section": section,
|
|
||||||
"config": subconffile,
|
|
||||||
}
|
|
||||||
|
|
||||||
fzf = FzfPrompt()
|
fzf = FzfPrompt()
|
||||||
selections = fzf.prompt(["Select one or more servers group to open"] + list(sections.keys()), "--cycle --multi --header-lines 1")
|
selected_group = fzf.prompt(sections.keys(), "--cycle")[0]
|
||||||
|
|
||||||
if not selections:
|
config = configs[sections[selected_group]]
|
||||||
sys.exit(0)
|
|
||||||
|
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)
|
||||||
|
|
||||||
# Get active tmux sessions
|
|
||||||
srv = libtmux.Server()
|
srv = libtmux.Server()
|
||||||
active_sessions = srv.attached_sessions
|
active_sessions = srv.sessions.filter(session_attached='1')
|
||||||
|
|
||||||
if parser_args.separate_sessions:
|
if active_sessions:
|
||||||
sessions = srv.sessions.filter(name="SSH-MultiG")
|
|
||||||
|
|
||||||
if sessions:
|
|
||||||
active_session = sessions[0]
|
|
||||||
|
|
||||||
else:
|
|
||||||
active_session = srv.new_session(session_name="SSH-MultiG")
|
|
||||||
|
|
||||||
else:
|
|
||||||
if len(active_sessions) > 1:
|
|
||||||
session_choice = fzf.prompt(["You have multiple active tmux sessions open. Choose one to create the window on"] + [sess.name for sess in active_sessions], "--cycle --header-lines 1")
|
|
||||||
|
|
||||||
if not session_choice:
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
active_session = srv.sessions.filter(name=session_choice[0])[0]
|
|
||||||
|
|
||||||
elif len(active_sessions) == 1:
|
|
||||||
active_session = active_sessions[0]
|
active_session = active_sessions[0]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raw_try = srv.cmd("display", "-p", "#{session_name}").stdout
|
raw_try = srv.cmd("display", "-p", "#{session_name}").stdout
|
||||||
if raw_try:
|
if raw_try:
|
||||||
active_session = srv.sessions.filter(name=raw_try[0])[0]
|
active_session = srv.sessions.filter(name=raw_try[0])[0]
|
||||||
@ -80,63 +51,30 @@ else:
|
|||||||
else:
|
else:
|
||||||
active_session = srv.sessions[0]
|
active_session = srv.sessions[0]
|
||||||
|
|
||||||
for selected_group in selections:
|
window = active_session.new_window(f"ssh-multig {','.join(servers)}", window_shell=f"ssh {servers[0]}")
|
||||||
|
|
||||||
info = sections[selected_group]
|
for server in servers[1:]:
|
||||||
config = configs[info["config"]]
|
|
||||||
servers = config[info["section"]]["servers"].split("\n")
|
|
||||||
|
|
||||||
extra_commands = []
|
|
||||||
if "commands" in config[info["section"]].keys():
|
|
||||||
extra_commands = config[info["section"]]["commands"].split("\n")
|
|
||||||
|
|
||||||
if parser_args.no_open:
|
|
||||||
print(selected_group)
|
|
||||||
print(f" - Servers ({len(servers)}):")
|
|
||||||
for server in servers:
|
|
||||||
print(f" * {server}")
|
|
||||||
|
|
||||||
if extra_commands:
|
|
||||||
print(f" - Extra commands ({len(extra_commands)}):")
|
|
||||||
for command in extra_commands:
|
|
||||||
print(f" * {command}")
|
|
||||||
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not servers:
|
|
||||||
continue
|
|
||||||
|
|
||||||
window_name = get_window_name(info["section"])
|
|
||||||
if windows := active_session.windows.filter(name=window_name):
|
|
||||||
windows[0].select()
|
|
||||||
continue
|
|
||||||
|
|
||||||
window = active_session.new_window(window_name, window_shell=f"ssh {servers[0]}")
|
|
||||||
|
|
||||||
for server in servers[1:]:
|
|
||||||
pane = window.split(shell=f"ssh {server}")
|
|
||||||
# Select tiled layout each time, to ensure enough space
|
|
||||||
window.select_layout("tiled")
|
window.select_layout("tiled")
|
||||||
|
pane = window.split(shell=f"ssh {server}")
|
||||||
|
|
||||||
# Wait until tmux finished working
|
# Wait until tmux finished working
|
||||||
time.sleep(0.05)
|
time.sleep(0.1)
|
||||||
|
|
||||||
# Confirm connection on asking panes
|
# Confirm connection on asking panes
|
||||||
confirmation_needed_text = "Are you sure you want to continue connecting (yes/no/[fingerprint])?"
|
confirmation_needed_text = "Are you sure you want to continue connecting (yes/no/[fingerprint])?"
|
||||||
for pane in window.panes:
|
for pane in window.panes:
|
||||||
pane_content = pane.capture_pane()
|
pane_content = pane.capture_pane()
|
||||||
if pane_content and confirmation_needed_text == pane_content[-1]:
|
if pane_content and confirmation_needed_text == pane_content[-1]:
|
||||||
pane.send_keys("yes")
|
pane.send_keys("yes")
|
||||||
|
|
||||||
window.set_window_option("synchronize-panes", "on")
|
window.set_window_option("synchronize-panes", "on")
|
||||||
pane = window.panes[0]
|
pane = window.panes[0]
|
||||||
pane.send_keys("sudo su -")
|
pane.send_keys("sudo su -")
|
||||||
|
|
||||||
for command in extra_commands:
|
for command in extra_commands:
|
||||||
pane.send_keys(command)
|
pane.send_keys(command)
|
||||||
|
|
||||||
window.set_window_option("synchronize-panes", "off")
|
window.set_window_option("synchronize-panes", "off")
|
||||||
window.select()
|
|
||||||
window.select_layout("tiled")
|
|
||||||
|
|
||||||
active_session.switch_client()
|
window.select()
|
||||||
|
srv.cmd("select-layout", "tiled")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user