Skip to content

Instantly share code, notes, and snippets.

@ahsankhatri
Last active March 6, 2018 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahsankhatri/35f27f44a51e12a7184fe41bb23c9369 to your computer and use it in GitHub Desktop.
Save ahsankhatri/35f27f44a51e12a7184fe41bb23c9369 to your computer and use it in GitHub Desktop.
A function to display and parse local or remote ip address (Mac, Linux & Win (cygwin) supported)
# View IP Address
myip() {
if [ -z "$1" ] || [ "$1" == "--help" ]; then
echo "Usage: myip OPTION
OPTIONS:
--remote displays remote (internet) IP Address
--local displays local IP Address(s)
--help display this help and exit
";
elif [ "$1" == "--remote" ]; then
# dig +short myip.opendns.com @resolver1.opendns.com
curl icanhazip.com
elif [ "$1" == "--local" ]; then
case "$(uname -s)" in
Darwin|Linux)
ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
;;
CYGWIN*|MINGW32*|MSYS*)
ipconfig | awk '$1 == "IPv4" {print $NF}'
;;
esac
else
echo "Invalid arguments supplied. Use --help for more information"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment