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

Display current network location via the Terminal Network
I make heavy usage of the Network Locations feature in OS X. It took me a lot of hunting to figure out (programatically) how to find my ocation. So after much work, here's a little Perl script that will return your location. I use this in shell scripts and Perl scripts to take actions conditional to my location.
#!/usr/bin/perl -w
use strict;

foreach (`scselect 2>&1`) {
    next unless /^\s*\*/;
    if (/\((\w+)/) {
        print "$1\n";
        exit;
    }
}

print "dunno\n";
Notice this is really just parsing the output of the scselect command. I haven't seen any documentation for this command, so I don't know if it's possible to do more with it.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[14,955 views]  

Display current network location via the Terminal | 13 comments | Create New Account
Click here to return to the 'Display current network location via the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Display current network location via the Terminal
Authored by: fivefifty on Sep 23, '04 11:16:56AM

playing around I found out that "scselect n" chooses the n-th location.



[ Reply to This | # ]
Display current network location via the Terminal
Authored by: romulis on Sep 23, '04 12:12:57PM

What sort of things do you do depending on your location?

I really love this aspect of OS-X (great for laptops) but it's still a matter of:

[bbbzzzzzzzttt]No connection to ...
Ahh! That's right, I'm at work now...
change location
rety
that's better

So what I'm missing is the "oops - no connection?! Let's see if we can find a network we know... yep, use this one instead" feature :-)



[ Reply to This | # ]
Display current network location via the Terminal
Authored by: Ferdy on Sep 23, '04 12:24:30PM
scselect 2>&1 | grep '^ \*' | sed -e 's:^[^(]*(\([^)]*\))$:\1:g'
I think this is much better.
Thanks.

[ Reply to This | # ]
Display current network location via the Terminal
Authored by: ua on Sep 23, '04 03:43:38PM
Shorter:
scselect | perl -lne 'print $1 if /^ \*.*\((.*)\)/'
Probably prints an error message if scselect fails. Probably prints nothing if no location is selected.

[ Reply to This | # ]
Display current network location via the Terminal
Authored by: Ferdy on Sep 24, '04 08:23:36AM
Hehe... i tried not to use perl because i don't relly know it. But it does not handle nested (. Anyway i modified a bit on mines and now it does:
sed 's:^[^(]*(\(.*\))$:\1:g'
though i dont like perl, yours is shorter :D Cheers,Ferdy

[ Reply to This | # ]
Display current network location via the Terminal
Authored by: VF on Dec 16, '04 01:31:36AM
Here is another way to do it (might be more human-readable...).

#!/bin/sh
scselect 2>&1 | grep '^[ ]*\*' | cut -d'(' -f2 | cut -d')' -f1
P.s.: Of course, also preserves SPACEs in your location-name.

---
VF

[ Reply to This | # ]

Display current network location via the Terminal
Authored by: m@ on Sep 23, '04 12:28:45PM
I use this apple script wraper for a couple of shell commands for my iChat status, so people know where i am and what time it is where i am:

set localtime to do shell script "date \"+%I:%M %p %Z\""
set location to do shell script "scutil <<EOF | sed -n 's/.*UserDefinedName : \\(.*$\\)/\\1/p'
open
show Setup:/
close
EOF"

return "@ " & location & " [" & localtime & "]"



[ Reply to This | # ]
Parentheses
Authored by: escowles on Sep 23, '04 12:33:49PM

If you use parens in your location names like I do (e.g., "Work (Airport)" or "Work (Ethernet)"), you'll want to do something like this:


#!/usr/bin/perl -w
use strict;

foreach (`scselect 2>&1`) {
    next unless /^\s*\*/;
    s/.*?\(//;
    s/\)$//;
    print;
    exit;
}

print "dunno\n";

This handles the nested parens properly.

-Esme



[ Reply to This | # ]
Display current network location via the Terminal
Authored by: tamás on Sep 23, '04 01:08:02PM
Here is the source of scselect as of OS X 10.3.5: scselect.c

[ Reply to This | # ]
Display current network location via the Terminal
Authored by: renderhead on Sep 23, '04 01:29:54PM
If you want to keep your location constantly displayed, in the menubar for instance, you could combine this hint with Geektool. Set up Geektool to run this script every n minutes and display the results in whatever font and location you want.

[ Reply to This | # ]
Display current network location via the Terminal
Authored by: PhlegmMaster on Sep 23, '04 01:36:11PM

The scselect tool is for making new and for changing your current location from the console (or any other shell-thing) -

Select Set Command
usage: scselect [-n] new-set-name



[ Reply to This | # ]
Display current network location via the Terminal
Authored by: scatlin on Sep 23, '04 03:08:33PM
Here's a script I wrote a couple of weeks ago when I discovered the scselect and scutil commands. I had some initial timing issues with DHCP, but I think I fixed that with a 'sleep 5'. It's fairly limited as it relies on a DHCP server at each "Location" and that the first three octets of the IP address be different. Hopefully someone can improve on it and make it more useful.

#!/bin/sh
###########################################################################
#
# select_network - A script to try and select the correct Network
#   Location under MacOS X.  Read down through the comments to see what
#   changes you may need to make for your system.
#
###########################################################################

function get_location {
    scutil <<- EOT | awk 'BEGIN { FS = " : "} /UserDefinedName/ { print $2 } END { }' > .Tmp$$
open
show Setup:/
close
quit

EOT

CURRENT_LOC=`cat .tmp$$`
rm -f .tmp$$
}

###########################################################################
#
# get_active_interface()
#
# Seaches through your list of network interfaces to find the first one
# that's active.  Sets $EN to that interfaces name, "ERROR" if no active
# interfaces are found.  For my laptop, en0 is builtin ethernet and
# en1 is the AirPort card.  Change to suite for your system.
#
function get_active_interface {
    for i in en0 en1
    do
      ifconfig $i 2> /dev/null | grep -q ": active"
      if [ $? = 0 ]; then
	  EN=$i
	  return
      fi
    done
    EN="ERROR"
}

# The executable that actually changes the Network Location in the Finder
SELECT="/usr/sbin/scselect"
LOG="$HOME/.network_select_log"

# Get the current Location for loggin purposes
get_location
echo "Current Location is $CURRENT_LOC" >> $LOG

# Change the location to Automatic.  On my system, this is the setting
# that uses DHCP to obtain the network address information.  I use this
# to help me determine where I am.
$SELECT Automatic >> $LOG 2>&1
#$SELECT Automatic > /dev/null 2>&1

sleep 5

get_active_interface # See comments for this function above

if [ $EN = "ERROR" ]; then
    echo "No active Network Interfaces" >> $LOG
    exit
fi

while [[ -z $ENADDR ]]; do
    # This gets the first three octets of the IP address for the network
    # interface found using the get_active_interface function
    ENADDR=`ifconfig $EN inet | grep inet | awk '{print $2}' | awk -F '.' '{print $1 "." $2 "." $3 }' 2> /dev/null`
done
echo "IP for Automatic is $ENADDR" >> $LOG

# In my case, the 192.168.4.* network is at work, and 10.10.10.* is home.
# Adjust this for your case.
case $ENADDR in
    	192.168.4  ) LOC="Work" ;;
	10.10.10   ) LOC="Home" ;;
	*  )         LOC="Automatic" ;;
esac
if [ $LOC != "Automatic" ]; then
    #$SELECT $LOC > /dev/null 2>&1
    $SELECT $LOC >> $LOG 2>&1
fi
echo "`date` - $CURRENT_LOC -> $LOC" >> $LOG


[ Reply to This | # ]
Display current network location via the Terminal
Authored by: thogard on Oct 07, '04 08:22:06AM

How about using arp -a to get the mac addresses of your router (via netstat -rn | grep default) and then use that to build a list of what the should be the correct location if you see that mac address in the mac table.

There was another hint that said there was a script that was run anytime the settings changes like you plugged in a network. tap into that, detect the mac addresses and then set the location and all the proxy settings will follow.



[ Reply to This | # ]