Get internal and external IP address on Mac with Bash
Frequently while coding I need to know the internal and external IP address of my development machine. For example, when your machine is serving a website from a locally running web server, the internal IP address can be used to test the site from a smartphone browser connected to the same network. Likewise, the external IP address is useful when you want to make a local web server publicly accessible or when you need to whitelist your IP address in the firewall settings of an external server.
To help with the above, I wrote a simple Bash function that will list the internal IP address for active wifi and ethernet connections as well as the external IP address on a Mac (tested on OS X Yosemite). When you run the function from the terminal, you’ll see something like this:
seanw@mac:~$ myip
Internal IP address (wired): none
Internal IP address (wifi): 192.168.0.2
External IP address: 41.52.154.137
To make this function available from the command-line, add the following to your .bash_profile
:
myip() {
external_ip=`dig +short myip.opendns.com @resolver1.opendns.com 2> /dev/null || echo none`
echo "Internal IP address (wired): "`ipconfig getifaddr en1 || echo none`
echo "Internal IP address (wifi): "`ipconfig getifaddr en0 || echo none`
echo "External IP address: $external_ip"
}
Next post
Google Drive: Publicly hosting a static website
Previous post
Download a Git subdirectory from GitHub