Create a gist now

Instantly share code, notes, and snippets.

anonymous /snippet.sh
Created Sep 5, 2011

What would you like to do?
#!/usr/bin/env ruby
require 'pp'
script_dir = File.expand_path(File.dirname(__FILE__))
$network_location_file_path = "#{script_dir}/network-location.txt"
def l s
puts s
end
def assigned_network_location
# example output: * 6064213B-532D-43C9-8941-DC72B6487955 (Work)
output = `scselect 2>&1 | grep ' \\* '`
# parse out the location
location = output.scan(/\(.*\)/).first.gsub(/[\(\)]/, "")
location
end
def active_network_location
location = 'home'
`ifconfig | grep -i "inet 54."`
if $? == 0 then
location = 'work'
end
location
end
def network_location_from_file
File.open($network_location_file_path, 'r').read.chomp
end
# look at ip address to determine active network
active = active_network_location
# get the system set network location in Network Preferences
assigned = assigned_network_location
# switch the assigned network location if they don't match
if active != assigned then
puts "setting network location to \"#{active}\""
# save active location to a file for other scripts to use
`echo #{active} > #{$network_location_file_path}`
# update network preferences setting
`scselect \"#{active}\"`
else
l "no network location changes"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment