Last active
February 14, 2019 19:45
-
-
Save mihaitodor/1e4048e14ca6cf7d5b9a90bd8a37028b to your computer and use it in GitHub Desktop.
ssh convenience wrapper script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Assume that the last argument is the address of the host | |
input_addr="${@: -1}" | |
do_ssh () { | |
ssh "$@" | |
ret_val=$? | |
if [[ ${ret_val} != 0 && ${ret_val} != 127 && ${ret_val} != 130 ]]; then | |
echo "Failed to connect to: ${input_addr}" | |
exit $ret_val | |
fi | |
exit 0 | |
} | |
# Remove preceding 'http(s)://', if detected | |
input_addr=${input_addr##http*://} | |
# Remove trailing slash, if any | |
input_addr=${input_addr%/} | |
# Convert from underscores to hyphens, if needed | |
if [[ "${input_addr}" =~ ^(dev|prod)_(singularity|mesos)_.*_ ]]; then | |
input_addr=${input_addr//_/-} | |
fi | |
# If the input address is a hostname composed of 4 words, get rid of any extra stuff | |
# unless we're dealing with a fully qualified domain... | |
if [[ "${input_addr}" =~ ^([a-zA-Z]+)-([a-zA-Z]+)-([a-zA-Z]+)-([a-zA-Z]+)(.?) ]]; then | |
if [ "${BASH_REMATCH[5]}" != "." ]; then | |
input_addr="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]}-${BASH_REMATCH[4]}" | |
fi | |
fi | |
# ip-10-3-18-52 -> 10.3.18.52 | |
# but don't match full DNS aliases, like ip-10-3-18-52.us-west-2.compute.internal | |
if [[ "${input_addr}" =~ ^ip-.*[0-9]{1,3}$ ]]; then | |
#input_addr=$(sed "s/ip-//; s/-/\./g;" <<< ${input_addr}) | |
#input_addr=$(sed "s/ip-(\d+)-(\d+)-(\d+)-(\d+)/\1.\2.\3.\4/" <<< ${input_addr}) | |
#input_addr=$(sed -nE "s/^ip-([[:digit:]])-([[:digit:]])-([[:digit:]])-([[:digit:]])$/\1.\2.\3.\4/p" <<< ${input_addr}) | |
#input_addr=$(echo -n ${input_addr} | cut -d - -f 2- | tr - .) | |
# Black magic: | |
# ${input_addr#ip-} trims the "ip-" prefix | |
# ${input_addr//-/.} replaces all occurences of "-" with "." | |
input_addr=${input_addr#ip-} | |
input_addr=${input_addr//-/.} | |
fi | |
# "${@:1:$(($#-1))}" contains all but the last argument passed to this script | |
do_ssh "${@:1:$(($#-1))}" ${input_addr} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment