9

Is it possible to query ~/.ssh/config in the command line? Let's say I would like to retrieve what IP address does the particular alias point to in a separate program, is it possible?

2

If I understand that you just want the IP address returned, ie., 192.168.1.1, then this is one (incredibly brittle) way of querying the file from the command line, provided you have the appropriate permissions to read it and your .ssh/config is consistently formatted:

awk '/Host $youralias/ {getline; print $2}' .ssh/config


I am only posting this as I would like to understand how to use awk to do this, but my knowledge is, obviously, quite limited.

1

Not totally sure what you are asking...

Say you have a SSH alias for 'mainserver' so you can do ssh mainserver. If you want it to also work for ping mainserver, you need to put an alias in /etc/hosts, like so:

/etc/hosts:

192.168.0.1 comp-one
192.168.0.2 comp-two

192.168.0.20 mainserver

Then you could do ping mainserver and it would ping 192.168.0.20.

0

This is a slightly more robust way of retrieving the HostName based on Host in the .ssh/config file.

sshalias()
{
    awk "\$1==\"Host\" {host=\$2} \$1==\"HostName\" && host==\"$1\" {print \$2}" "$HOME/.ssh/config"
}
-1

The file you mention is a text file, it contains host names and possibly IP addresses to identify the machines you connect to. Host names can be resolved to IP addresses using the host(1) command. The format is quite simple, so it isn't too hard to find out what is in there. Note that wildcards can be used in the file (mine states *.fedoraproject.org).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.