12

I have an SSH config file like:

Host myAlias
  HostName the.actual.host.name.com

Is it possible to resolve the hostname from the alias, from the shell and without connecting to the host? I'm aiming for something like:

$ <something> myAlias
the.actual.host.name.com
Joe Kearney
  • 7,397
  • 6
  • 34
  • 45

1 Answers1

24

While it is possible to parse the ~/.ssh/config with a simple awk script, it might not work for arbitrary config file, which may have various blocks, etc. Consider instead using the 'ssh -G', which will dump the parameters of an ssh session, then extract the hostname attribute

ssh -G myAlias | awk '$1 == "hostname" { print $2 }'

Note that this has the advantage of supporting all ssh configuration sources (local config, command line, global config, environment variables, etc).

Just for completeness, quick and dirty awk solution

awk -v H="myAlias" '
tolower($1) == "host" { m=$2 == H }
tolower($1) == "hostname" && m{ print $2 }
' ~/.ssh/config
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • 1
    That's great, I wasn't aware of `-G`. Thanks. – Joe Kearney Dec 12 '19 at 13:51
  • 1
    This is awesome. The `-G` option doesn't show in the help text, and isn't listed in the online man page, but is in my local man page. For those wondering: `-G Causes ssh to print its configuration after evaluating Host and Match blocks and exit.` – alwaysmpe Oct 19 '21 at 12:36