Skip to content

Instantly share code, notes, and snippets.

@ke4roh
Created May 28, 2015 13:54
Show Gist options
  • Save ke4roh/05f591ee6cf8e053add0 to your computer and use it in GitHub Desktop.
Save ke4roh/05f591ee6cf8e053add0 to your computer and use it in GitHub Desktop.
Determine if the given host is localhost
#!/bin/bash
set -e
function isLocalhost {
local TEST_IP=$(getent hosts $1 | cut -f1 -d\ | head -1)
local ALL_MY_IPS=$(ip addr | grep inet | grep -v link | cut -f2- -de | cut -f2 -d\ | cut -f1 -d/)
( echo $ALL_MY_IPS | grep -q $TEST_IP ) && return 0
return 1
}
if ( isLocalhost $1 ) ; then
echo yes
else
echo no
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment