Use ~/.ssh/config
This commit is contained in:
parent
b9bb606cdd
commit
7954aeff70
39
sshtui
39
sshtui
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
import subprocess, curses, configparser
|
||||
import subprocess, curses
|
||||
from pathlib import Path
|
||||
|
||||
# constants
|
||||
|
@ -13,8 +13,6 @@ HEADER_HEIGHT = 4 # in rows
|
|||
FOOTER_HEIGHT = 3
|
||||
ROWS_HEIGHT = curses.LINES - HEADER_HEIGHT - FOOTER_HEIGHT - 3 # maximum number of visible rows indices
|
||||
WIDTH = curses.COLS
|
||||
CONFIG = configparser.ConfigParser()
|
||||
CONFIG.read(f"{str(Path.home())}/.sshtui.ini")
|
||||
|
||||
|
||||
# classes
|
||||
|
@ -150,22 +148,35 @@ def enable_mouse_support():
|
|||
print('\033[?1003h') # enable mouse tracking with the XTERM API. That's the magic
|
||||
|
||||
|
||||
def get_servers() -> list:
|
||||
servers = []
|
||||
server_names = CONFIG.sections()
|
||||
for server_name in server_names:
|
||||
user = CONFIG[server_name]['User']
|
||||
server = CONFIG[server_name]['Server']
|
||||
identity = CONFIG[server_name]['Identity'] if 'Identity' in CONFIG[server_name] else "~/.ssh/id_rsa"
|
||||
port = CONFIG[server_name]['Port'] if 'Port' in CONFIG[server_name] else "22"
|
||||
servers.append(f"{server_name:<20} {user:<20} {server:<30} {port:<8} {identity}")
|
||||
return servers
|
||||
def get_servers() -> dict:
|
||||
with open(f"{str(Path.home())}/.ssh/config", "r") as f:
|
||||
lines = f.readlines()
|
||||
current_host = None
|
||||
hosts = {}
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line.startswith("Host "):
|
||||
current_host = line.split()[1]
|
||||
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():
|
||||
global menus, selected_menu, user, server, identity, port
|
||||
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]
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue