Skip to content

Instantly share code, notes, and snippets.

@rk295
Last active October 12, 2016 09:45
Show Gist options
  • Save rk295/606c6a7e80ff4f39e81f306832a508eb to your computer and use it in GitHub Desktop.
Save rk295/606c6a7e80ff4f39e81f306832a508eb to your computer and use it in GitHub Desktop.
If you use [consul](https://www.consul.io/) this will add a bash auto complete function to help you ssh to servers shown by `consul members list`
#
# consul_ssh-completion.sh
#
# Adds all servers returned by `consul members list` to the ssh command as
# tab completion candidates.
#
# Usage:
#
# curl -OL https://gist.githubusercontent.com/rk295/606c6a7e80ff4f39e81f306832a508eb/raw/825a539d6693a6c0535110e7c7a88d1211234e63/consul_ssh-completion.sh ; . consul_ssh-completion.sh
#
#
# Author: Robin Kearney <robin@kearney.co.uk>
# Date: 21/04/2016
#
#
_consul_ssh()
{
local cur prev opts
consulPath=$(command -v consul)
# Catch case where consul can't be found.
if [[ $? -ne 0 ]]; then
return 0
fi
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(consul members -status=alive | grep -v ^Node | cut -d ' ' -f 1)
COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _consul_ssh ssh
@rk295
Copy link
Author

rk295 commented Apr 21, 2016

Usage:

Curl the raw version of this file (url) and then source it:

curl -sOL https://gist.githubusercontent.com/rk295/606c6a7e80ff4f39e81f306832a508eb/raw/1575d310dfb604030a3779b933b42f1e59e46522/consul_ssh-completion.sh
. consul_ssh-completion.sh

Maybe add it to /etc/bash_completion.d/ or wherever your distro puts such things or source it from your personal profile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment