Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2011 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1195922 to your computer and use it in GitHub Desktop.
Save anonymous/1195922 to your computer and use it in GitHub Desktop.
#!/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