Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created May 15, 2015 10:05
Show Gist options
  • Save sathishvj/f7e1c28d3ad7d24e8d80 to your computer and use it in GitHub Desktop.
Save sathishvj/f7e1c28d3ad7d24e8d80 to your computer and use it in GitHub Desktop.
bash function to ssh into a server with a matching name in /etc/hosts
# this worked for me on the Mac's shell
# to search for a string in hosts file
function ho() {
cat /etc/hosts | grep $1
}
# ssh into a server if there is a single entry that matches. Or otherwise list names that match.
function hoc() {
count=`cat /etc/hosts | grep $1 | wc -l`
if [ $count -ne 1 ]; then
ho $1
else
srvr=`cat /etc/hosts | grep $1 | sed -e "s/[[:space:]]\{1,\}/ /g" | cut -f2 -d " "`
echo "ssh-ing to " $srvr
ssh $srvr
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment