Created
December 13, 2014 14:38
-
-
Save matb33/dedb9da53e87a8f25561 to your computer and use it in GitHub Desktop.
Utility script to get the primary local IP address on your system. Works in Linux and OSX.
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
#!/bin/bash | |
if [ "$(which ipconfig)" != "" ]; then | |
for i in $(ifconfig -ul); do | |
if [[ $i != lo* ]]; then | |
ipaddr=$(ipconfig getifaddr $i) | |
if [ "$ipaddr" != "" ]; then | |
echo $ipaddr | |
exit 0 | |
fi | |
fi | |
done | |
exit 1 | |
fi | |
if [ "$(which ip)" != "" ]; then | |
for i in $(ifconfig -s | grep -v lo | grep -v Iface | awk '{print $1}'); do | |
ipaddr=$(ip -4 -o addr show $i | awk 'BEGIN{FS="[ /]+"}{print $4}') | |
if [ "$ipaddr" != "" ]; then | |
echo $ipaddr | |
exit 0 | |
fi | |
done | |
exit 1 | |
fi | |
exit 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment