Skip to content

Instantly share code, notes, and snippets.

@olomor
Created January 20, 2023 05:11
Show Gist options
  • Select an option

  • Save olomor/a9e49c5640fc8c73f9a647d4bd15ba70 to your computer and use it in GitHub Desktop.

Select an option

Save olomor/a9e49c5640fc8c73f9a647d4bd15ba70 to your computer and use it in GitHub Desktop.
Generates a option menu from the list of entries at "/etc/hosts" then starts a ssh connection command for selected item number.
#!/bin/bash
export PATH=/bin:/sbin:/usr/local/bin:/usr/local/sbin
while read -a E
do
IPADDRESS_LIST=( ${IPADDRESS_LIST[@]} "${E[0]}" )
HOSTNAME_LIST=( ${HOSTNAME_LIST[@]} "${E[1]}" )
done < <(egrep -v "^127.0.0.1|^::1" /etc/hosts |sort -k2 -k1)
unset E
echo -e "\n${0}"
echo "+-----------------------------------------------+"
echo "| Known Host List from '/etc/hosts' |"
echo "+-----------------------------------------------+"
for N in ${!HOSTNAME_LIST[@]}
do
echo "$N) ${HOSTNAME_LIST[$N]} (${IPADDRESS_LIST[$N]})"
done
unset N
echo "+-----------------------------------------------+"
echo "| Type the related number and press ENTER |"
echo "| or press CTRL+C for back to shell. |"
echo "+-----------------------------------------------+"
for E in ${!HOSTNAME_LIST[@]}
do
CHOICE_MAX=$E
done
unset E
while true
do
read -s -n $((CHOICE_MAX%10)) CHOICE
[[ ${!HOSTNAME_LIST[@]} =~ $CHOICE ]] && break
done
ssh -o "StrictHostKeyChecking=no" -o "GlobalKnownHostsFile=/dev/null" -o "TCPKeepAlive=yes" root@${HOSTNAME_LIST[$CHOICE]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment