Given the alias, get the HostName from ssh config
Asked Answered
L

1

13

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
Lobscouse answered 12/12, 2019 at 13:20 Comment(0)
L
29

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
Liebermann answered 12/12, 2019 at 13:39 Comment(2)
That's great, I wasn't aware of -G. Thanks.Lobscouse
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.Leora

© 2022 - 2024 — McMap. All rights reserved.