Skip to content

Instantly share code, notes, and snippets.

@bdowling
Last active July 13, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdowling/6df15cdf2db8afb4f6b37742ec9b5e8a to your computer and use it in GitHub Desktop.
Save bdowling/6df15cdf2db8afb4f6b37742ec9b5e8a to your computer and use it in GitHub Desktop.
Kinda simple but awesome way to ssh to hosts by IP but using the known_hosts name cache, e.g. if DNS is down or otherwise not resolving.
# yes, there are many ways to simpify this, but it's just a quick hack...
getsship() { fgrep $( fgrep "$1" ~/.ssh/known_hosts |head -1| awk '{print $3}') ~/.ssh/known_hosts | egrep -o '(^[0-9\.]+)'; }
sshbyip() { name=$1; shift; ssh $(getsship "$name") "$@"; }
# and if you want tab completion (assuming _ssh tab completion loaded)
# this test doesn't work, not sure yet what best way to copy the complete from 'ssh' comand is yet given how it is dynamically loaded.
# and not avaialble at .bashrc loading.. if type _ssh >/dev/null; then
if shopt -q progcomp && [ -n "$BASH_COMPLETION_COMPAT_DIR" ]; then
complete -F _ssh sshbyip
complete -F _ssh getsship
fi
# Usage:
# $ sshbyip somecachedhostname
# $ ssh dave@$(getsship daveshost)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment