I created a shitty Python script to manage multiple SSH connections because I couldnt find a decent one

https://git.ohaa.xyz/Oha/ssh-man

57 points · 12 comments · view on lemmy.world

12 Comments

heartsofwar@lemmy.world · 45 pts · 2y (9 replies)
[ removed ]
ShortN0te@lemmy.ml · 10 pts · 2y

Just on the side, Openssh and ssh config works just as well on Windows.

Oha@lemmy.ohaa.xyz · 2 pts · 2y (6 replies)

I doubt this was meant for linux...

It is. You cant get ssh to print out a nice list afaik.

forwardvoid@feddit.nl · 15 pts · 2y

Great attempt on making a tool, I think your usecase might not be as appealing to others. If I need to list the hosts I have config for I would use: grep Host ~/.ssh/config If your list of servers is too long to remember, you might want to look at Ansible for configuration. But whatever works for you :)

zacher_glachl@lemmy.world · 12 pts · 2y

I can (and do) just read the ~/ssh/.config file if needed, it's quite legible. In most cases however zsh autocompletion does all the heavy lifting for me (ssh ser(tab) -> ssh servername).

Still a cool idea for a script, and if it works well for you more power to you, just saying there's more ergonomic and universally applicable solutions. (Only mentioning this since you said "I couldn't find a decent solution to this problem").

aard@kyu.de · 8 pts · 2y (2 replies)

You have a list of systems you've connected to in known_hosts, though. And the config file is easy enough to parse - throwing away the stuff you don't care about - to expand on that list.

Oha@lemmy.ohaa.xyz · 2 pts · 2y (1 reply)

I could add a import from known_hosts option or something like that

aard@kyu.de · 5 pts · 2y

I assume you mean "lookup", as import doesn't really make much sense.

I'm currently using this with wofi, though I'll eventually rewrite it as anyrun plugin, which provides a bit more control:

#!/usr/bin/env python3
from argparse import ArgumentParser
import subprocess
import json
import os
 
ssh_config_file = "~/.ssh/config"
ssh_known_hosts_file = "~/.ssh/known_hosts"
 
# Returns a list of all hosts
def get_hosts():
 
    hosts = []
 
    with open(os.path.expanduser(ssh_config_file)) as f:
        content = f.readlines()
 
    for line in content:
        line = line.lstrip()
        # Ignore wildcards
        if line.startswith('Host ') and not '*' in line:
            for host in line.split()[1:]:
                hosts.append(host)
 
    # Removes duplicate entries
    hosts = sorted(set(hosts))
 
    return hosts
 
def get_known_hosts():
 
    hosts = []
 
    with open(os.path.expanduser(ssh_known_hosts_file)) as f:
        content = f.readlines()
 
    for line in content:
        line = line.lstrip()
        host_entry = line.partition(" ")[0]
        hosts.append(host_entry.partition(",")[0])
 
    # Removes duplicate entries
    hosts = sorted(set(hosts))
 
    return hosts
 
# Returns a newline seperated UFT-8 encoded string of all ssh hosts
def parse_hosts(hosts):
    return "\n".join(hosts).encode("UTF-8")
 
# Executes wofi with the given input string
def show_wofi(command, hosts):
 
    process = subprocess.Popen(command,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
    ret = process.communicate(input=hosts)
    host, rest = ret
    return host
 
# Switches the focus to the given id
def ssh_to_host(host, terminal, ssh_command):
 
    if "]:" in host:
        host, port = host[1:].split("]:")
        command = "{terminal} \'{ssh_command} {host} -p {port}\'".format(terminal=terminal, ssh_command=ssh_command, host=host, port=port)
    else:
        command = "{terminal} \'{ssh_command} {host}\'".format(terminal=terminal, ssh_command=ssh_command, host=host)
 
    process = subprocess.Popen(command,shell=True)
 
# Entry point
if __name__ == "__main__":
 
    parser = ArgumentParser(description="Wofi based ssh launcher")
    parser.add_argument("terminal", help='Terminal command to use')
    parser.add_argument("--ssh-command", dest='ssh_command', default='ssh', help='ssh command to use (default=ssh)')
    parser.add_argument("--mode", dest='mode', default='known_hosts', help='where to read from (default=known_hosts)')
    parser.add_argument("--command", default='wofi -p \"SSH hosts: \" -d -i --hide-scroll', help='launcher command to use')
    args = parser.parse_args()
 
    if (args.mode == "config"):
        hosts = get_hosts()
    elif (args.mode == "known_hosts"):
        hosts = get_known_hosts()
 
    parsed_hosts = parse_hosts(hosts)
 
    selected = show_wofi(args.command, parsed_hosts)
 
    selected_host = selected.decode('utf-8').rstrip()
 
    if selected_host != "":
        ssh_to_host(selected_host, args.terminal, args.ssh_command)
heartsofwar@lemmy.world · 5 pts · 2y
[ removed ]
Illecors@lemmy.cafe · 0 pts · 2y

☝️

axum@kbin.social · 5 pts · 2y (2 replies)

Multiple ssh connections should really just be managed using Ansible.

Oha@lemmy.ohaa.xyz · 14 pts · 2y (1 reply)

isnt ansible a tool for automatically deploying stuff?

falsem@kbin.social · 5 pts · 2y

Configuration management.

ryannathans@aussie.zone · 2 pts · 2y

Wrap a nice ssh config manager around kitty ssh https://sw.kovidgoyal.net/kitty/kittens/ssh/ and it'd be pretty slick