HACKER Q&A
📣 ilovecaching

Is there a hostname manager utility?


I am often wrangling hostnames in DCs, at my own home, in cloud providers, etc. and I'd like to find a program that can keep a database of these hostnames and metadata about them, make it searchable via the name or metadata, and provide an id so I can run something like ssh `hm 454` instead of copying and pasting large hostnames.

Are there any tools out there that are similar to what I'm describing?


  👤 theamk Accepted Answer ✓
a .yaml file in your favorite editor?

For metadata search, use regular text search in editor.. A prefix to avoid false positives comes handy, for example "s:aws" instead of "aws". You can also have nested structs to group your hosts widely.

For querying, tiny scripts in your favorite language.. the idea is to have many one-off scripts which do exactly what you need, instead of having one super-script that does it all. Here is an example of one of my scripts, which looks up IP based on human-readable name. (I could probably do it in one line of "yq" as well)

    #!/usr/bin/python3 -B
    # given a "name" in arguments, return corresponding IP
    import yaml, os, sys
    s = yaml.safe_load(open(os.path.expanduser("~/data/hostnames.yaml")))
    match = [info["ip"] for group in s.values() for name, info in group.items() if name in sys.argv[1:]]
    if len(match) != 1: sys.exit("name not found")
    print(match[0])
Yours would be different based on your requirements.

The tricky part is sometimes you need a printable report -- for this I export to csv and then open in editor. Not perfect, but works.


👤 kennethko
Are you looking for something beyond what a host alias in your `./ssh/config` file can provide?

  $ cat ~/.ssh/config
  Host myserver
          HostName 10.10.10.10
          User ken
  $ ssh myserver