How to get the Host value inside ~/.ssh/config

Posted by iconoclast on Server Fault See other posts from Server Fault or by iconoclast
Published on 2014-08-22T18:21:11Z Indexed on 2014/08/22 22:23 UTC
Read the original article Hit count: 200

Filed under:

Within a ~/.ssh/config or ssh_config file, %h will give you the HostName value, but how do you get the Host ("alias") value?

Why would I want to do that? Well, here's an example

  Host some_host_alias
    HostName 1.2.3.4
    User my_user_name
    PasswordAuthentication no
    IdentityFile ~/.ssh/some_host_alias.rsa.id
    LocalCommand some_script.sh %h    # <---- this is the critical line

If I pass %h to the script, then it uses 1.2.3.4, which fails to give it all the options it needs to connect to that machine. I need to pass some_host_alias, but I can't find the % variable for that.

(And: yes! I'm aware of the risk of recursion. That's solved inside the script.)


UPDATE: Kenster pointed out that I could just hard-code the Host value as an argument to the script. Of course this will work in the example I gave, but it won't work if I'm using pattern matching for the Host.

© Server Fault or respective owner

Related posts about ssh