Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to standardize my server aliases across projects by linking together ssh config files that I can keep under version control.

I'd like to be able to just define machine aliases along with IP addresses and ssh keys all in one place (the ssh configs). This works nicely with ssh based utils like /bin/ssh and (I believe) scp. You even get tab completion.

However, if I set the aliases in my ssh config then the aliases aren't available to ping and other vanilla network utils. I don't want to have to keep my /etc/hosts synchronized with my ssh config files.

Is there a way to either

a) get ssh aliases to automatically work as if they were in /etc/hosts. or b) find a suite of network utils that uses the active ssh config as a source of aliases.

?

share|improve this question
    
ssh_config is only referenced by ssh utilities. Why not use /etc/hosts to standardise - what are you storing in ssh_config beyond the hostname? – Paul May 27 '12 at 22:12
    
You can modify your shell completion setup to use ssh config but if these names does not resolve then you are still out of luck. I suggest sync /etc/hosts file too if you can't set up your own DNS server for that. – Cougar May 28 '12 at 7:32
    
@Paul late reply but, reasons for the above would be something like bastions were you would need to use ProxyCommand to forward over the bastion to actually talk to the sever. That's actually how I stumbled upon this question ;) – ehime Jul 30 '15 at 15:16
    
@ehime Still, the approach doesn't make sense. The /etc/hosts or a dns server is appropriate for storing name to IP address conversions, and ssh_config for managing ssh related things to do with those hosts. If there are many hosts across many servers, then use a public DNS server or deploy a private one with a master/slave across sites. – Paul Jul 30 '15 at 23:45

I wrote a small shell function that does exactly that and can be placed e.g. in your .bashrc.

It replaces ping and does the lookup of the last argument (host) in .ssh/config before calling the original /bin/ping, i.e. ping -c 2 <host> will call /bin/ping -c2 <hostname> where <hostname> is the matching IP/hostname in .ssh/config. If no host matching the last argument is found in the file, the original value is used as with standard ping.

ping()
{
    # Process args
    local i=0
    local options=""
    local host=""
    for arg
    do
        i=$(($i+1))
        if [ "$i" -lt "$#" ]
        then
            options="${options} ${arg}"
        else
            host="${arg}"
        fi
    done

    # Find host
    local hostname=$(awk "\$1==\"Host\" {host=\$2} \$1==\"HostName\" && host==\"${host}\" {print \$2}" "$HOME/.ssh/config")
    if [ -z "$hostname" ]
    then
        hostname="$host"
    fi

    # Run ping
    /bin/ping $options $hostname
}
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .