Created
September 5, 2011 21:06
-
-
Save anonymous/1195922 to your computer and use it in GitHub Desktop.
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 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