27

By default ifconfig will show me all available interfaces , but what if I just want to display active ones? Like, en0 only in below.

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 14:10:9f:e0:eb:c9 
    inet6 fe80::1610:9fff:fee0:ebc9%en0 prefixlen 64 scopeid 0x4 
    inet X.X.X.X netmask 0xffffff00 broadcast 101.6.69.255
    nd6 options=1<PERFORMNUD>
    media: autoselect
    **status: active**
en3: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    options=60<TSO4,TSO6>
    ether 32:00:14:e7:4f:80 
    media: autoselect <full-duplex>
    **status: inactive**

Notice ifconfig en0 will not satisfy, en0 is not always the active one ;)

I'm running Mac OS X.

2
  • Hi @jordanm, my goal is to to get IP address of currently active interface, often a wireless or wired Ethernet interface.
    – qweruiop
    Dec 1, 2013 at 6:18
  • how do you define the active interface? Dec 4, 2018 at 18:43

10 Answers 10

25

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.

2
  • @gavin-s-yancey your solution has a couple of problems. First, it shows all the interfaces without an active IP address. Second, egrep is not doing what it is supposed to, just getting the first result. I would suggest this: <code>ifconfig | pcregrep -M -o '([^\n]|\n\t)+inet ([^\n]|\n\t)+status: active'</code>
    – lasantha
    May 2, 2020 at 1:13
  • @lasantha The question asks about "active" interfaces, which is a property of an interface on MacOS, regardless of whether the interface has an IP associated with it. My regex finds the interface(s) marked as active. May 4, 2020 at 5:39
4

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.

1
  • This should be the best answer, however ifconfig -lu will not work on debian. But OP is using MacOS anyway, so this solution would work on systems with BSD's ifconfig. I posted another answer below which does the same without using ifconfig -lu
    – Rakib Fiha
    Nov 28, 2019 at 10:11
2

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
1

Reading your comments and question, it seems you actually want to just get the interfaces that have an IP address assigned to them.

You can do this quickly with ifconfig and grep.

Running the command:

ifconfig | grep 'Link\|inet'

Should produce something similar to:

eth0      Link encap:Ethernet  HWaddr 00:11:22:33:44:55
eth1      Link encap:Ethernet  HWaddr 00:11:22:33:44:66  
          inet addr:192.168.0.8  Bcast:192.168.0.255  Mask:255.255.255.0
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0

This would show each line with a interface name and and IP if they had one.

Some more specific REGEX magic might get you exactly what you need.

2
  • 2
    Thanks but where is active consideration ?
    – qweruiop
    Dec 1, 2013 at 15:22
  • if there is a line after the interface, then it has an IP address assigned to it, and by what I gather you mean, it would constitute as 'active'. Dec 27, 2013 at 17:14
1

Following will print out only those interfaces that are configured to have an IP

ifconfig | grep "inet.*broadcast"
1

I wish the question was a bit clearer. Assuming that by Active interface only, you meant to show the name of active interfaces only (In my case UP and RUNNING status active) without any other details. Then try this:

ifconfig | \
  grep "UP\|RUNNIG" \
  | awk '{print $1}' \
  | grep ':' \
  | tr -d ':' \
  | grep -v lo

The output would look like:

en0
en1
en2
bridge0
p2p0
awdl0
llw0
utun0
utun1
utun2
utun3
en6

Adjust the grep -v field and add in more unwanted interfaces like loopback --> lo etc, if necessary.

Now, if you want and you would need to check whether the interfaces found are active at present. Then we can just simply pass them as an argument for ifconfig <interface> like shown below:

[[ $(ifconfig en0 | grep -w active) ]] && echo "en0 is active"

Simplifying this further and implementing it in a function:

checkActiveInterfaces()
{
  local intARR=( $(ifconfig | \
      grep "UP\|RUNNIG" \
      | awk '{print $1}' \
      | grep ':' \
      | tr -d ':' \
      | grep -v lo) );
    intARR=( $(printf "%s\n" "${intARR[@]}" | sort -u) );
    for i in "${intARR[@]}"; do
      [[ $(ifconfig $i | grep -w active) ]] && {
       echo "$i"
    }
    done
}

Executing this function will return only the active interfaces. In my case:

awdl0
en0
llw0
1

I had to do this recently where it wasn't known if eth0 or eth1 would be up, but the system does not have pcregrep.

Here was my solution:

for interface in /sys/class/net/*; do [ `cat $interface/operstate` == "up" ] && ifconfig `basename $interface`; done

operstate gets checked first, and if it matches up then run ifconfig on it.

0

Get ACTIVE interface MAC only:

cat /sys/class/net/$(ip addr | awk '/state UP/ {print $2}' | sed 's/.$//')/address
1
0

netstat -i will show you a list of all active interfaces, networks, addresses (ip4, ip6, MAC), and a few more columns of stats on macOS versions ranging from High Sierra (10.13) to Monterey (12.3) (the 2 systems I have easy access to).

% netstat -i
Name       Mtu   Network       Address            Ipkts Ierrs    Opkts Oerrs  Coll
lo0        16384 <Link#1>                       7680002     0  7680002     0     0
lo0        16384 127           localhost        7680002     -  7680002     -     -
lo0        16384 localhost   ::1                7680002     -  7680002     -     -
lo0        16384 mac-manager fe80:1::1          7680002     -  7680002     -     -
en0        1500  <Link#6>    a4:83:e7:73:2b:b8  4393235     0  2854958     0     0
en0        1500  mac-manager fe80:6::1479:f3f9  4393235     -  2854958     -     -
en0        1500  192.168.162   192.168.162.196  4393235     -  2854958     -     -
en1        1500  <Link#7>    82:98:60:06:e0:01        0     0        0     0     0
en2        1500  <Link#8>    82:98:60:06:e0:00        0     0        0     0     0
:
:
llw0       1500  fe80::6c3f: fe80:b::6c3f:2fff        0     -        0     -     -
utun0      1380  <Link#12>                            0     0        2     0     0
utun0      1380  mac-manager fe80:c::8eb3:ce45        0     -        2     -     -
:
:
utun10     1380  <Link#26>                            0     0        3     0     0
utun10     1380  mac-manager fe80:1a::7e37:30c        0     -        3     -     -
en4        1500  <Link#22>   00:50:b6:9b:20:7a   178041     0     8093     0     0
en4        1500  mac-manager fe80:16::106d:4e6   178041     -     8093     -     -
en4        1500  10.1.2/24     macmanager.myne   178041     -     8093     -     -
-1
$ ifconfig | grep "inet addr:" | grep -v 127.0.0.1 | sed -e 's/Bcast//' | cut -d: -f2

How it works:

  • ifconfig
  • Grep for lines containing "inet addr"
    • These lines contains the IPs.
  • Grep for lines that do not contain "127.0.0.1"
    • We usually do not care about localhost.
    • -v is inverted grep
  • From remaining lines, remove the "Bcast"
  • Cut field 2 using ":" as a delimiter
    • Prints the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.