Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Get active network service name of default route UNIX
I've written a script that gets the name of the network service that is currently associated with the default route. The network service name is not the same as the interface name (typically en0, en1), and exists to provide a human-readable reference to a *logical* network service. This is an important distinction, since there may be more than one logical network service per interface (e.g. interface aliases, etc).

The ability to obtain the network service name programatically is useful in conjuntion with networksetup (see my previous hint on that). In my case, I'm working on a script that opens a socks proxy with ssh -D. I want the script to also insert the necessary configuration at the system level, so that my applications will then use the proxy. Getting the current network service name means that the script will work no matter how I am connected to the Internet. This is because the network service name must be supplied to networksetup when modifying the proxy configuration, and the network service name may change with network location. I will possibly post that script once it's finished, but I think this is useful as it stands for rolling your own scripts around networksetup.

I couldn't get carriage returns to be interpreted correctly in the echo statements (well, I couldn't after 10 minutes of trying, then I gave up), which makes this messier than it needs to be. I tried several combinations of printf and echo, but I couldn't get the newline to come through. printf would have worked except it apparently doesn't do variable interpolation. Feel free to post any improvements as comments here.
    •    
  • Currently 2.80 / 5
  You rated: 2 / 5 (5 votes cast)
 
[10,516 views]  

Get active network service name of default route | 10 comments | Create New Account
Click here to return to the 'Get active network service name of default route' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Improved script
Authored by: thrig on Feb 18, '05 12:40:37PM

SERVICE_GUID=`cat <<EOF | scutil | grep PrimaryService | awk '{print $3}'
open
get State:/Network/Global/IPv4
d.show
EOF`

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

echo $SERVICE_GUID $SERVICE_NAME


[ Reply to This | # ]
Improved script possible bug!
Authored by: thrig on Feb 18, '05 12:42:36PM
grep "UserDefinedName" | \  awk

Was supposed to be on two lines, so you might need to remove the backslash.



[ Reply to This | # ]
improved script (using a Bash function)
Authored by: hayne on Feb 18, '05 01:25:33PM

#!/bin/sh

scutil_query()
{
    key=$1

    scutil<<EOT
    open
    get $key
    d.show
    close
EOT
}

SERVICE_GUID=`scutil_query State:/Network/Global/IPv4 | grep "PrimaryService" | awk '{print $3}'`

SERVICE_NAME=`scutil_query Setup:/Network/Service/$SERVICE_GUID | grep "UserDefinedName" | awk -F': ' '{print $2}'`

echo $SERVICE_NAME


[ Reply to This | # ]
improved script (using a Bash function)
Authored by: dreness on Feb 18, '05 01:37:22PM

very nice, thanks a bunch :)



[ Reply to This | # ]
Call to grep unnecessary
Authored by: gvaughn on Feb 18, '05 02:17:52PM

This can also be simplified by getting rid of grep. awk itself contains a pattern matching engine as powerful as grep.

grep "PrimaryService" | awk '{print $3}'

can be replaced with:

awk '/PrimaryService/ { print $3}'



[ Reply to This | # ]
Get active network service name of default route
Authored by: joshturse on Feb 18, '05 02:36:09PM

I'm a bit of newbie, and am more curious about the "starting network services" portion of this post. Could something like this script be used to open an SSH tunnel to fix the broken afp-ssh in Panther?



[ Reply to This | # ]
Get active network service name of default route
Authored by: CarlRJ on Feb 18, '05 03:57:49PM
The bit that you were missing with the newlines is that echo in bash (at least) wants a "-e" option to enable backslash escapes. This simplifies things a bit:

SERVICE_GUID=`
    echo -e "open\nget State:/Network/Global/IPv4\nd.show" |
    scutil |
    awk '/PrimaryService/{print $3}'
`

SERVICE_NAME=`
    echo -e "open\nget Setup:/Network/Service/$SERVICE_GUID\nd.show" |
    scutil |
    awk -F': ' '/UserDefinedName/{print $2}'
`

echo $SERVICE_NAME


[ Reply to This | # ]
Get active network service name of default route
Authored by: LC on Feb 20, '05 08:38:11PM
The following is not as compact, but there's a small bit of error checking (exit status) built in --
#!/bin/sh
sc_q() {
        /usr/sbin/scutil <<-EOM
                open
                get ${1}
                close
                d.show
        EOM
        }

sc_f() {
        pat="${1}"
        while read scu ; do
                set -f X ${scu} ; shift
                [[ "X${1}" = "X${pat}" ]] || continue
                shift ; eval echo \"\${$#}\"
                return 0
        done
        return 1
        }

s_id=$(sc_q "State:/Network/Global/IPv4" | sc_f "PrimaryService") || exit 1
sc_q "Setup:/Network/Service/${s_id}" | sc_f "UserDefinedName" || exit 1


[ Reply to This | # ]
Get active network service name of default route
Authored by: overhacked on Feb 20, '05 11:57:55PM

Hi,

I'm wondering what the Service name attribute is actually related to. Is it a hashed representation of the "Location" setting available through system preferences?

How can one employ this setting for nifty purposes? I've had lots of location-based scripting ideas in the past, and I'm wondering how exactly this Service Type attribute may benefit my ideas.

Thanks!



[ Reply to This | # ]
Get active network service name of default route
Authored by: matamatangi on Feb 21, '05 01:08:57AM

the horror of those "|||"! printf works fine:

SERVICE_GUID=`printf "open\nget State:/Network/Global/IPv4\nd.show" | \
scutil | grep "PrimaryService" | awk '{print $3}'`


SERVICE_NAME=`printf "open\nget Setup:/Network/Service/$SERVICE_GUID\nd.show" |\
scutil | grep "UserDefinedName" | awk -F': ' '{print $2}'`

echo $SERVICE_NAME



[ Reply to This | # ]