Created
May 2, 2016 13:27
-
-
Save signed8bit/3da09a6778d94686470f6e41dee6f9cc to your computer and use it in GitHub Desktop.
A simple wrapper script for setting a network location on OS X using the networksetup command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
## | |
# Switches to the specified network location | |
## | |
function switch_location() { | |
if ! /usr/sbin/networksetup -switchtolocation "${loc}" >/dev/null 2>&1 ; then | |
echo "Error: Network location \"${loc}\" not found" | |
fi | |
} | |
## | |
# Lists the available network locations | |
## | |
function list_locations() { | |
networksetup -listlocations | |
} | |
## | |
# Prints usage information | |
## | |
function usage() { | |
echo "Usage: netloc [-l] [-h] location" | |
echo "Example: netloc work" | |
} | |
## | |
# Parse options | |
## | |
while getopts ':lh' opt; do | |
case ${opt} in | |
l) | |
list_locations | |
exit 0 | |
;; | |
h) | |
usage | |
exit 0 | |
;; | |
\?) | |
echo "Error: -$OPTARG is an invalid option" >&2 | |
usage | |
exit 1 | |
;; | |
:) | |
echo "Error: -$OPTARG requires an argument" >&2 | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
## | |
# Parses the required operand | |
## | |
shift $((OPTIND-1)) | |
if [[ $# -lt 1 ]]; then | |
usage | |
exit 1 | |
fi | |
## | |
# Must be run as root or via sudo | |
## | |
if [ "$EUID" -ne 0 ]; then | |
echo "Error: Must be run as the superuser" | |
exit 1 | |
fi | |
loc=${1} | |
switch_location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment