1

I am trying to list network interface that I am currently using. I need to know how to do this in terminal, and in script.

ifconfig | grep $(networksetup -listnetworkserviceorder | grep 'Ethernet, Device' | sed -E "s/.*(en[0-9]).*/\1/")

What am i getting: enter image description here

What do I want to get: Only the name of active interface

Szymon Korbel
  • 13
  • 1
  • 1
  • 4

2 Answers2

1

Use your second command, but put everything after the interface name into a lookahead, so it's not printed as part of the match.

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

When I do this, the output is:

en0
awdl0

See What is AWDL (Apple Wireless Direct Link) and how does it work? for what awdl0 is.

Your first command should work if just print the result of the networksetup pipeline, without using ifconfig. This works for me:

networksetup -listnetworkserviceorder | grep 'Wi-Fi, Device' | sed -E "s/.*(en[0-9]).*/\1/"

I have Wi-Fi rather than Ethernet.

Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can also use the scutil command to show active interfaces and their IPv4/6 addresses (though it doesn't show the AWDL interfaces):

scutil --nwi
Pierz
  • 7,064
  • 52
  • 59