bashsshssh-config

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

Solution

  • 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