Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



MacOS - Finding act...
 
Share:
Notifications
Clear all

[Solved] MacOS - Finding active ethernet/wifi ports with ifconfig

2 Posts
1 Users
0 Reactions
6,359 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2869
Topic starter  

I had to figure a way to see what ethernet or wifi ports were actively connected.
Here 'ifconfig' could have been great but it provides a LOT of data. So here a one-liner for Terminal to see the active connections:

ifconfig | grep flags=8863 | grep -v bridge

The output will look something like this:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

Here we see en0 and en1 being active (my Ethernet ports), or for example

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
en2: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

where en2 is my WiFi.

If you'd like to see the IPv4 address as well, then try this:

ifconfig | grep 'flags=8863\|inet ' | grep -v 'bridge\|127.0.0.1'

Which will list something like this:

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
 inet 192.168.2.147 netmask 0xffffff00 broadcast 192.168.2.255
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
 inet 192.168.2.244 netmask 0xffffff00 broadcast 192.168.2.255
en2: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
 inet 192.168.2.245 netmask 0xffffff00 broadcast 192.168.2.255

Here you see only the active network connections (Ethernet and WiFi) and the IPv4 IP address.

Note: I'm no expect on the flags. You can find the flag details in the **if.h** header file - Spotlight is your friend in finding "if.h". I found mine for example here:

/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/net/if.h

which will show you what the flags mean (keep in mind: hexadecimal);

    #define IFF_UP          0x1             /* interface is up */
    #define IFF_BROADCAST 0x2 /* broadcast address valid */
    #define IFF_DEBUG 0x4 /* turn on debugging */
    #define IFF_LOOPBACK 0x8 /* is a loopback net */
    #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */
    #define IFF_NOTRAILERS 0x20 /* obsolete: avoid use of trailers */
    #define IFF_RUNNING 0x40 /* resources allocated */
    #define IFF_NOARP 0x80 /* no address resolution protocol */
    #define IFF_PROMISC 0x100 /* receive all packets */
    #define IFF_ALLMULTI 0x200 /* receive all multicast packets */
    #define IFF_OACTIVE 0x400 /* transmission in progress */
    #define IFF_SIMPLEX 0x800 /* can't hear own transmissions */
    #define IFF_LINK0 0x1000 /* per link layer defined bit */
    #define IFF_LINK1 0x2000 /* per link layer defined bit */
    #define IFF_LINK2 0x4000 /* per link layer defined bit */
    #define IFF_ALTPHYS IFF_LINK2 /* use alternate physical connection */
    #define IFF_MULTICAST 0x8000 /* supports multicast */

So 8863 means;

MULTICAST (0x8000) + SIMPLEX (0x800) + RUNNING (0x40) + NOTRAILERS (0x20) + BROADCAST (0x2) + UP (0x1).

Interested in know what en0, en1, etc stands for?

Try this:

networksetup -listnetworkserviceorder

Example output:

An asterisk (*) denotes that a network service is disabled.
(1) Ethernet 1
(Hardware Port: Ethernet 1, Device: en0)
(2) Ethernet 2
(Hardware Port: Ethernet 2, Device: en1)
(3) Wi-Fi
(Hardware Port: Wi-Fi, Device: en2)
(4) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en9)
(5) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2869
Topic starter  

As an alternative one could use:

scutil --nwi

which produces a list like this:

Network information
IPv4 network interface information
     en0 : flags : 0x5 (IPv4,DNS)
           address : 192.168.2.147
           reach : 0x00000002 (Reachable)
     en1 : flags : 0x5 (IPv4,DNS)
           address : 192.168.2.244
           reach : 0x00000002 (Reachable)
     en2 : flags : 0x5 (IPv4,DNS)
           address : 192.168.2.245
           reach : 0x00000002 (Reachable)
   REACH : flags 0x00000002 (Reachable)
IPv6 network interface information
   No IPv6 states found

   REACH : flags 0x00000000 (Not Reachable)
Network interfaces: en0 en1 en2


   
ReplyQuote
Share: