this post was submitted on 13 Nov 2023
60 points (90.5% liked)

Linux

46852 readers
2260 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Oha@lemmy.ohaa.xyz 3 points 9 months ago* (last edited 9 months ago) (4 children)

I doubt this was meant for linux...

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

[–] forwardvoid@feddit.nl 15 points 9 months ago

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 points 9 months ago* (last edited 9 months ago)

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 points 9 months ago (1 children)

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 1 points 9 months ago (1 children)

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

[–] aard@kyu.de 4 points 9 months ago

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)