Bash Scripting: Network Locations

Hi all. Welcome back to my (hopefully) popular Bash scripting series!
In this batch I plan to go through some items which I learnt for a recent education project: modifying network locations via the command line, modifying proxy settings via the command line, and combining the two into a possible first run script that can be used at imaging time to set these items as desired.

Working with networksetup

All over, the command in this series will use the ‘networksetup’ command
working with network setup
In today’s blog, I will be dealing with the arguments relating to Network location setup.

Detailing what’s already there

The first thing you might want to do, is to check what is the current set Network location. Although not useful in an initial ‘first run’ script (as this will almost always be ‘Automatic’) this might be handy for an extension attribute in Casper, or to have your script check and report back.

networksetup -getcurrentlocation

To get the GUI name of the current network location simply use the following command for my computer, I get the following output:
gui for current network location
This corresponds with the name of my current location name of “Work”:
current network location
Right, so you now know what the current network location is, but say you want to know what all possible network locations are? Simply use:

networksetup -listlocations

For my computer I get:
network setup list location
Corresponding to my System Preferences window:
system preferences window
What’s the point? Well, for an example, say you set every laptop to have two locations: one “Home” with no proxies, and one “School” with lots of proxies. To avoid confusion you could write your script to check for the presence of the “Automatic” location and remove it, followed by adding the two new locations.

Adding and removing network locations

Okay, so now you know how to check what the current network location is, and how to check what all locations are. Now let’s do something with that information.
So next step, you want to add a new location called “Home”. You also want to autofill it with the default services (i.e. Ethernet, Wi-Fi, Firewire as per machine hardware) so you’ll need to use the “populate” argument. Use the following command:

networksetup –createlocation "Home" populate

As this is a change affecting the entire OS you will need to preface these with sudo to run it as root.
So you’ve found out the default location is “Automatic”, and this is the only location, and you’ve created the new location you require. Next step is to switch the current location to the new “Home” one. Simple use this:

networksetup –switchtolocation "Home"

Again, this is changing a system-wide setting and needs root to run it.
Now, you have switched to the new location, there’s nothing stopping you from removing the default “Automatic” location. Use this:

networksetup –deletelocation "Automatic"

It’s scripting time!

All make sense so far? Good because I’m assuming it does (if not, drop me a comment below).
Let’s take all of the above and combine it into a script to make things more automated:

#!/bin/bash
## Load Variables ##
CurrentLocation=`networksetup -getcurrentlocation`
CheckForAutoLocation=`networksetup -listlocations | grep -c "Automatic"`
################################################################
if [ "$CurrentLocation" != "Automatic" ]
	then
		echo "Current location is not Automatic"
		echo "Unexpected value, has this been run before?"
		exit 1
	else
		echo "Current location is Automatic"
		echo "Continuing Script"
fi
################################################################
if [ "$CheckForAutoLocation" != "0" ]
	then
		echo "Automatic location does not exist"
		echo "Unexpected value, has this been run before?"
		exit 1
	else
		echo "Automatic location is present"
		echo "Continuing Script"
fi
################################################################
echo "Creating Home location and auto populating network interfaces"
networksetup –createlocation "Home" populate
	if [ $? -ne 0 ]
			then
				currentdate=`date`
				echo "Creating Home location failed. Please Investigate."
				exit 1
			else
				currentdate=`date`
				echo "Home location created fine, continuing script"
	fi
################################################################
echo "Switching to the new  Home location"
networksetup –switchtolocation "Home"
	if [ $? -ne 0 ]
			then
				currentdate=`date`
				echo "Switching to the Home location failed. Please Investigate."
				exit 1
			else
				currentdate=`date`
				echo "Home location switched fine, continuing script"
	fi
################################################################
echo "Deleting the Automatic location"
networksetup –deletelocation "Automatic"
	if [ $? -ne 0 ]
			then
				currentdate=`date`
				echo "Deleting the Automatic location failed. Please Investigate."
				exit 1
			else
				currentdate=`date`
				echo "Automatic location deleted fine, continuing script"
	fi
exit 0

Summary

There we go. I hope that this has given you some starting points or ideas on configuring your network locations using the command line, scripts or payload-free packages.
Any hints, tips or opinions? Let us know in the comments below and I’ll try to respond to as many as I can.