Created
May 15, 2015 10:05
-
-
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 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
# 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