#!/usr/bin/env python3 """List SSH host names from ~/.ssh/config, following Include. VS Code Remote-SSH shows every Host name from the main file and from files pulled in with Include. A search of only the main file misses those. OpenSSH has no command that prints that list. ssh -G looks up one host. This reads the config on this computer and prints one name per line. It skips patterns that contain * or ?, including Host *. It does not print addresses, users, or keys, and it does not send anything. python3 ssh-hosts.py python3 ssh-hosts.py --config /path/to/config python3 ssh-hosts.py --self-test """ import glob import os import shlex import sys import tempfile from pathlib import Path def _strip_comment(line): out = [] quote = None for ch in line: if quote: out.append(ch) if ch == quote: quote = None continue if ch in "\"'": quote = ch out.append(ch) continue if ch == "#": break out.append(ch) return "".join(out).strip() def _keyword(line): text = _strip_comment(line) if not text: return None, "" parts = text.split(None, 1) key = parts[0].lower() rest = parts[1] if len(parts) > 1 else "" return key, rest def _tokens(rest): try: return shlex.split(rest) except ValueError: return rest.split() def _wildcard(name): return ("*" in name) or ("?" in name) or name.startswith("!") def _include_paths(pattern, ssh_dir): expanded = os.path.expanduser(pattern) if not os.path.isabs(expanded): expanded = str(Path(ssh_dir) / expanded) matches = glob.glob(expanded) if matches: return sorted(matches) if any(ch in expanded for ch in "*?["): return [] return [expanded] def host_names(config_path): """Return host names in the order they appear. config_path is the user config.""" config_path = Path(config_path).expanduser() ssh_dir = config_path.parent seen = set() names = [] seen_names = set() def walk(path): try: real = Path(path).resolve() except OSError: return if real in seen or not real.is_file(): return seen.add(real) try: lines = real.read_text(encoding="utf-8", errors="replace").splitlines() except OSError: return for line in lines: key, rest = _keyword(line) if key == "include": for token in _tokens(rest): for child in _include_paths(token, ssh_dir): walk(child) elif key == "host": for token in _tokens(rest): if _wildcard(token) or token in seen_names: continue seen_names.add(token) names.append(token) walk(config_path) return names def _self_test(): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) (root / "keys").mkdir() (root / "extra").mkdir() (root / "config").write_text( "\n".join([ "Include extra/more_config", "Include keys/*.conf", "Include missing-file", "Host VM", " HostName 192.168.50.200", "Host *", " User fallback", "# Host hidden", ]), encoding="utf-8", ) (root / "extra" / "more_config").write_text( "\n".join([ "Include extra/again", 'Host web "lab box"', "Host *.internal", ]), encoding="utf-8", ) (root / "extra" / "again").write_text("Host VM\nHost again\n", encoding="utf-8") (root / "keys" / "a.conf").write_text("Host db\n", encoding="utf-8") (root / "keys" / "b.conf").write_text("Host ?\nHost files\n", encoding="utf-8") got = host_names(root / "config") expect = ["VM", "again", "web", "lab box", "db", "files"] if got != expect: sys.stderr.write("got %s\nexpected %s\n" % (got, expect)) return 1 print("PASS", " ".join(got)) return 0 def main(argv): if "--self-test" in argv: return _self_test() path = Path.home() / ".ssh" / "config" if "--config" in argv: i = argv.index("--config") path = Path(argv[i + 1]) for name in host_names(path): print(name) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))