Skip to content

Instantly share code, notes, and snippets.

@matb33
Created December 13, 2014 14:38
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 matb33/dedb9da53e87a8f25561 to your computer and use it in GitHub Desktop.
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.
#!/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