Skip to content

Instantly share code, notes, and snippets.

@dotancohen
Forked from yaronuliel/sshhosts.sh
Created January 1, 2018 10:06
Show Gist options
  • Save dotancohen/1406af2fb3acc988c4a0858d40bd4334 to your computer and use it in GitHub Desktop.
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
#!/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