Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given the alias, get the HostName from ssh config

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
like image 918
Joe Kearney Avatar asked Dec 12 '19 13:12

Joe Kearney


1 Answers

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
like image 198
dash-o Avatar answered Sep 20 '22 01:09

dash-o