Use ~/.ssh/config

This commit is contained in:
Digital Studium 2024-05-07 10:01:49 +03:00
parent b9bb606cdd
commit 7954aeff70
1 changed files with 25 additions and 14 deletions

39
sshtui
View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess, curses, configparser import subprocess, curses
from pathlib import Path from pathlib import Path
# constants # constants
@ -13,8 +13,6 @@ HEADER_HEIGHT = 4 # in rows
FOOTER_HEIGHT = 3 FOOTER_HEIGHT = 3
ROWS_HEIGHT = curses.LINES - HEADER_HEIGHT - FOOTER_HEIGHT - 3 # maximum number of visible rows indices ROWS_HEIGHT = curses.LINES - HEADER_HEIGHT - FOOTER_HEIGHT - 3 # maximum number of visible rows indices
WIDTH = curses.COLS WIDTH = curses.COLS
CONFIG = configparser.ConfigParser()
CONFIG.read(f"{str(Path.home())}/.sshtui.ini")
# classes # classes
@ -150,22 +148,35 @@ def enable_mouse_support():
print('\033[?1003h') # enable mouse tracking with the XTERM API. That's the magic print('\033[?1003h') # enable mouse tracking with the XTERM API. That's the magic
def get_servers() -> list: def get_servers() -> dict:
servers = [] with open(f"{str(Path.home())}/.ssh/config", "r") as f:
server_names = CONFIG.sections() lines = f.readlines()
for server_name in server_names: current_host = None
user = CONFIG[server_name]['User'] hosts = {}
server = CONFIG[server_name]['Server'] for line in lines:
identity = CONFIG[server_name]['Identity'] if 'Identity' in CONFIG[server_name] else "~/.ssh/id_rsa" line = line.strip()
port = CONFIG[server_name]['Port'] if 'Port' in CONFIG[server_name] else "22" if line.startswith("Host "):
servers.append(f"{server_name:<20} {user:<20} {server:<30} {port:<8} {identity}") current_host = line.split()[1]
return servers hosts[current_host] = {}
elif line and line[0].isupper():
key, value = line.split(" ")
key = key.strip()
value = value.strip()
hosts[current_host][key] = value
return hosts
def init_menus(): def init_menus():
global menus, selected_menu, user, server, identity, port global menus, selected_menu, user, server, identity, port
menu_title = '{: <20s} {: <20s} {: <30s} {: <8s} {}'.format("Name", "User", "Host", "Port", "Identity") menu_title = '{: <20s} {: <20s} {: <30s} {: <8s} {}'.format("Name", "User", "Host", "Port", "Identity")
menus = [Menu(menu_title, get_servers(), 0, WIDTH, ROWS_HEIGHT)] menu_rows = []
for host, params in get_servers().items():
user = params['User']
server = params['HostName']
port = params.get('Port', "22")
identity = params.get('IdentityFile', '~/.ssh/id_rsa')
menu_rows.append(f"{host: <20s} {user: <20s} {server: <30s} {port: <8s} {identity}")
menus = [Menu(menu_title, menu_rows, 0, WIDTH, ROWS_HEIGHT)]
selected_menu = menus[0] selected_menu = menus[0]
user = lambda: menus[0].selected_row().split()[-4] if menus[0].selected_row() else None user = lambda: menus[0].selected_row().split()[-4] if menus[0].selected_row() else None
server = lambda: menus[0].selected_row().split()[-3] if menus[0].selected_row() else None server = lambda: menus[0].selected_row().split()[-3] if menus[0].selected_row() else None