12

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

  1. get ssh aliases to automatically work as if they were in /etc/hosts? or
  2. find a suite of network utils that uses the active ssh config as a source of aliases?
4
  • 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, 2012 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, 2012 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, 2015 at 15:16
  • 1
    @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, 2015 at 23:45

2 Answers 2

6

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
}
3
  • That awk line didn't work for me. After much trial and error this sed did local hostname=$(sed -En '/./{H;$!d} ; x ; s/host\s+'${host}'.*hostname\s+(\S+)/\1/pI' "$HOME/.ssh/config")
    – Martin
    Oct 5, 2020 at 15:17
  • Also if you add this after the function then you can auto complete your new improved ping from the list of hosts in the config complete -W "$(grep -Ei 'host\s+[^\s*]+\s*$' ~/.ssh/config | sed -E 's/host\s+//i')" ping
    – Martin
    Oct 5, 2020 at 16:30
  • Actually the grep in my previous comment has a weird bug. [^\s*] in GNU grep apparently does not mean NOT whitespace or literal star. It actually means NOT backslash, s or literal star. You need grep -Pi 'host\s+[^\s*]+\s*$' or you miss out on servers with an s in them!
    – Martin
    Oct 29, 2020 at 11:26
0

pip3 install sshping

then sshping hostname

2
  • 1
    (1) OK, but that works just for ping, right?  It appears that you’ve made the mistake of answering the question title, and not reading the entire question, which mentions “other vanilla network utils” (route, nmap, netcat and traceroute come to mind).  The other answer can easily be adapted to handle any commands that the OP wants to use. (2) You should link to documentation that supports your answer. Oct 26, 2020 at 23:17
  • You can do that if you want. Nov 10, 2020 at 16:20

You must log in to answer this question.

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