Skip to content

Instantly share code, notes, and snippets.

@failable
Forked from reorx/networkservice.sh
Created March 15, 2025 06:22
Show Gist options
  • Select an option

  • Save failable/46d20ee0ca1d850d273a9bfecf51b28d to your computer and use it in GitHub Desktop.

Select an option

Save failable/46d20ee0ca1d850d273a9bfecf51b28d to your computer and use it in GitHub Desktop.

Revisions

  1. @reorx reorx created this gist Jun 27, 2017.
    28 changes: 28 additions & 0 deletions networkservice.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/bin/bash

    services=$(networksetup -listnetworkserviceorder | grep 'Hardware Port')

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

    if [ -n "$currentservice" ]; then
    echo $currentservice
    echo $currentdevice
    echo $currentmac
    else
    >&2 echo "Could not find current service"
    exit 1
    fi