How to find the currently connected network service from the command line? I would like to find out which of the available network services (e.g. Ethernet or Wi-Fi) is currently active. In this screenshot from the Network Preferences, you can see that Wi-Fi is currently active (the green dot):



How can I get that information from the command line?

The networksetup command allows me to list the available network services:

$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Ethernet
FireWire
Wi-Fi


It can also show some details on the service, like the device name:

$ networksetup -listnetworkserviceorder
An asterisk (*) denotes that a network service is disabled.
(1) Ethernet
(Hardware Port: Ethernet, Device: en0)

(2) FireWire
(Hardware Port: FireWire, Device: fw0)

(3) Wi-Fi
(Hardware Port: Wi-Fi, Device: en1)


Unfortunately, the info on which service is active (the green dot from the screenshot) is not available in this info. Is there another command that I could use to get this information?
Put it all together, I wrote a script to accomplish this task:
#!/bin/bash

while read -r line; do
    sname=$(echo &quot;$line&quot; | awk -F  &quot;(, )|(: )|[)]&quot; '{print $2}')
    sdev=$(echo &quot;$line&quot; | awk -F  &quot;(, )|(: )|[)]&quot; '{print $4}')
    #echo &quot;Current service: $sname, $sdev, $currentservice&quot;
    if [ -n &quot;$sdev&quot; ]; then
        ifout=&quot;$(ifconfig &quot;$sdev&quot; 2&gt;/dev/null)&quot;
        echo &quot;$ifout&quot; | grep 'status: active' &gt; /dev/null 2&gt;&amp;1
        rc=&quot;$?&quot;
        if [ &quot;$rc&quot; -eq 0 ]; then
            currentservice=&quot;$sname&quot;
            currentdevice=&quot;$sdev&quot;
            currentmac=$(echo &quot;$ifout&quot; | awk '/ether/{print $2}')

            # may have multiple active devices, so echo it here
            echo &quot;$currentservice, $currentdevice, $currentmac&quot;
        fi
    fi
done &lt;&lt;&lt; &quot;$(networksetup -listnetworkserviceorder | grep 'Hardware Port')&quot;

if [ -z &quot;$currentservice&quot; ]; then
    &gt;&amp;2 echo &quot;Could not find current service&quot;
    exit 1
fi

The script first get a service list from networksetup command, then check if each service is in active status from ifconfig.
Name the script to networkservice.sh for example, then execute it to get the current network service you are on.
$ bash networkservice.sh
USB 10/100/1000 LAN, en4, 00:e0:4a:6b:4d:0c
Wi-Fi, en0, 8c:85:90:a0:4b:ec