How to use ifconfig to show active interface only

To get a complete description of all the active services, try:

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

This simple regex should filter out only active interfaces and all their information. I sugest you put an alias for this in your ~/.profile or ~/.bash_profile file (maybe ifactive?)

To just get the interface name (useful for scripts), use:

ifconfig | pcregrep -M -o '^[^\t:]+:([^\n]|\n\t)*status: active' | egrep -o -m 1 '^[^\t:]+'

You have to install pcregrep for this to work. It's on macports in the pcre package. Alternatively, this should work with GNU grep using grep -Pzo instead of pcregrep -M -o but with the rest the same, but I haven't tested this.

If you are not adverse to some bash scripting, you can do this:

for i in $(ifconfig -lu); do if ifconfig $i | grep -q "status: active" ; then echo $i; fi; done

That's will list out the active network interfaces. Tested on Mac OS X 10.13.

The nice thing is that you don't need to install anything. Just run the above in a Terminal.

If you only want to print the “entry” if it contains status: active, then you could use something like this awk program as a filter to the ifconfig output:

#!/usr/bin/awk -f
BEGIN            { print_it = 0 }
/status: active/ { print_it = 1 }
/^($|[^\t])/     { if(print_it) print buffer; buffer = $0; print_it = 0 }
/^\t/            { buffer = buffer "\n" $0 }
END              { if(print_it) print buffer }

When each “entry” starts (a line is empty or does not start with a Tab), start saving the entry in a buffer. Append to this buffer any subsequent lines that start with a Tab. Watch for the magic string status: active; if a line like that was seen, print out the buffer (the previous “entry”) when a new “entry” starts (or the input ends).

Save the above program text in a file and use it like this:

ifconfig -a | awk -f /path/to/file

Or, if you chmod +x the file, then you can simplify it a bit:

ifconfig -a | /path/to/file