-
-
Save dotancohen/1406af2fb3acc988c4a0858d40bd4334 to your computer and use it in GitHub Desktop.
Connect to preconfigure ssh hosts (set in ~/.ssh/config) via simple list with numbers
This file contains hidden or 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
#!/usr/bin/env bash | |
get_hosts="cat ~/.ssh/config | grep Host\\\\s | awk '{ print \$2 }' | sort -u" | |
if [[ $# -eq 0 ]] ; then | |
eval "$get_hosts | nl -ba" | |
exit; | |
fi | |
host_num=$1 | |
re='^[0-9]+$' | |
if ! [[ $host_num =~ $re ]] ; then | |
echo "error: Not a number" >&2 | |
exit 1 | |
fi | |
host=$(eval "$get_hosts | sed -n '${host_num}p'") | |
if [[ -z $host ]] ; then | |
echo "Error: invalid host number" >&2 | |
exit 1 | |
fi | |
echo "Connecting $host" | |
ssh $host |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment