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