Last active
March 6, 2018 20:37
-
-
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)
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
# 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