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


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: 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 | # ]