Determining Local IPv4 and IPv6 Addresses On Mac OS X Via The Command Line

Getting IPv4 and IPv6 addresses of your local machine on Mac OS X 10.8 Mountain Lion is quite easy. Here is how it works:

IF='en0' # ← restrict to wired connection or do  IF=""  to get all
# Get local IPv4 addresses (without loopback):
ifconfig $IF |grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'
# Get globally routable v6 IPs
ifconfig $IF | grep -E 'inet6.[0-9a-f]' | grep -v -E 'inet6 f[0cde]' | awk '{ print $2}'

Comments