Is there a utility to ping a server using aliases in ssh config (instead of /etc/hosts)?

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.

?


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
}