#!/bin/sh
# Script for getting the network service name which holds the default route
# dre@mac.com, 2/14/05   (with some help from mikeash)

# First we get the generated unique ID of the network service that is currently
# set as the default route. Because scutil doesn't have command line switches
# for quering specific keys, we use echo to send commands to scutil's STDIN,
# as a way of simulating the commands we would type interactively to achieve
# the same result (tr is used to convert ||| into carriage return)

SERVICE_GUID=`echo "open|||get State:/Network/Global/IPv4|||d.show" | \
tr '|||' '\n' | scutil | grep "PrimaryService" | awk '{print $3}'`

# next we use SERVICE_GUID to perform another query to scutil, this time to get
# the human-readable name associated with the given network service. This name
# could contain spaces and the like, so we switch awk's field seperator to ": "
# to nab everything after the colon.

SERVICE_NAME=`echo "open|||get Setup:/Network/Service/$SERVICE_GUID|||d.show" |\
tr '|||' '\n' | scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`

echo $SERVICE_NAME
