How to find the currently connected network service from the command line?
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