Skip to content

Instantly share code, notes, and snippets.

@fifar
Created June 29, 2020 08:07
Show Gist options
  • Save fifar/b4ea6bc276eaebf9c3781990ee7dff34 to your computer and use it in GitHub Desktop.
Save fifar/b4ea6bc276eaebf9c3781990ee7dff34 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Black 0;30 Dark Gray 1;30
# Red 0;31 Light Red 1;31
# Green 0;32 Light Green 1;32
# Brown/Orange 0;33 Yellow 1;33
# Blue 0;34 Light Blue 1;34
# Purple 0;35 Light Purple 1;35
# Cyan 0;36 Light Cyan 1;36
# Light Gray 0;37 White 1;37
RED='\033[0;31m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
PLAIN='\033[0m'
# Change it to your path
REPO_PATH=<empty>
function get_host() {
env=${2:-staging}
path=$REPO_PATH/inventory/${env}/*
raw=$(grep $1 $path 2>/dev/null)
candidates=$(echo "$raw" | awk -F':' '{print $2}' | grep -o -E '^\s{0,}[0-9\.]+')
[[ -z "$candidates" ]] && {
echo "Can't get any hosts from $path"
exit 0
}
num=$(echo "$candidates" | wc -l)
if [[ $num == 1 ]]; then
echo -e "${RED}Trying to connect to ${candidates} ($1)${PLAIN}"
ssh $candidates
else
echo -e "${BLUE}${raw}${PLAIN}"
index=0
for h in $(echo "$candidates"); do
echo -e "${GREEN}[${index}] ${h}${PLAIN}"
((index+=1))
done
read -p "Choose a host [0]: " idx
index=${idx:-0}
((index+=1))
host=$(echo "$candidates" | sed -e "${index}q;d")
echo -e "${RED}Trying to connect to ${host} ($1)${PLAIN}"
ssh $host
fi
}
get_host "$@"
@fifar
Copy link
Author

fifar commented Jun 29, 2020

  • Usage: jump <keyword-in-service-name>
  • Example: jump airflow
  • Note: Move jump to your PATH

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