Determine the Active Network Interface and IP address from Terminal on a MAC

On a MAC (OSX) Using the command line (Terminal) often you want a quick way to determine the Active Network Interfaces along with the IP address.

Here is a quick ‘alias’ you can make to get the IP(s)

# scutil --nwi|awk 'BEGIN {s=0} $0 ~ /^IPv4/ {s=1;next} s==1 && $0 ~ /flags/ {intf=$1;next} s==1 && $1 ~ /address/ {ip=$NF; print intf,ip} $1 ~ /REACH/{exit}'

Example Output

# scutil --nwi|awk 'BEGIN {s=0} $0 ~ /^IPv4/ {s=1;next} s==1 && $0 ~ /flags/ {intf=$1;next} s==1 && $1 ~ /address/ {ip=$NF; print intf,ip} $1 ~ /REACH/{exit}'
en9 192.168.1.88

Creating a small bash script in /usr/local/bin/ so you can reference it anytime.

#!/bin/bash
# scutil --nwi|awk 'BEGIN {s=0} $0 ~ /^IPv4/ {s=1;next} s==1 && $0 ~ /flags/ {intf=$1;next} s==1 && $1 ~ /address/ {ip=$NF; print intf,ip} $1 ~ /REACH/{exit}'

If you wish to only view the active interfaces on MAC OS you can do the following and create an alias for it

#ifconfig | pcregrep -M -o '^[^\t:]+(?=:([^\n]|\n\t)*status: active)'

Example Output

# ifconfig | pcregrep -M -o '^[^\t:]+(?=:([^\n]|\n\t)*status: active)'
en9
en6
This entry was posted in MAC-OSX. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *