Tuesday, 21 July 2015

How to get the IP address of a Mac

Every time you connect a new network adapter to a Mac, you get a new Ethernet device configured to your computer. This is of both logical and expected, but it means that you can't really tell what the name of your currently active network interface is. This makes things like checking your IP address a bit cumbersome.

And here's a shell script to remedy:

interface=$(route get default| awk '/interface: / {print $2}')
ipconfig getifaddr $interface

This will only return the IP address of your default NIC. If you have more than one active network interface, you'll have to take the long route :)