Skip to content

Instantly share code, notes, and snippets.

@sofakartoffel
Last active December 15, 2015 00:49
Show Gist options
  • Save sofakartoffel/5175899 to your computer and use it in GitHub Desktop.
Save sofakartoffel/5175899 to your computer and use it in GitHub Desktop.
OSX seems to set 802.11d country code upon the first found network's country code. This might not be the correct one, depending on your or your neighbors' settings. This script restarts your Wi-Fi until (or 10 times) the correct country code is set.
-- This does not work if you need administrator privilieges for setting Wi-Fi power
set correctcountrycode to "DE"
repeat 10 times
set countrycode to do shell script "system_profiler SPAirPortDataType -xml | xmllint --dropdtd - | xpath '//key[text()=\"spairport_wireless_country_code\"]/following-sibling::string[1]/text()'"
if countrycode = correctcountrycode then exit repeat
do shell script "networksetup -setairportpower en1 off"
delay 6
do shell script "networksetup -setairportpower en1 on"
delay 2
end repeat
@sofakartoffel
Copy link
Author

Does not work on Mountain Lion

@alpacapaca
Copy link

this works in Mountain Lion - I switched correct country code for bad one, as my cosmopolitan neighbourhood has a friendly DE router and bane-of-my-existence TW router, which invariably gets picked up by airport and takes out my 5ghz channels.

# Bad Country Code
set badcountrycode to "TW"

# Fetch the name of your AirPort Device
set airPortDevice to do shell script "/usr/sbin/networksetup -listallhardwareports | awk '{if($3==\"Wi-Fi\"){getline;print}}' | awk '{print $2}'"

repeat 10 times
    set countrycode to do shell script "system_profiler SPAirPortDataType | egrep -m 1 'Country Code:.[A-Z][A-Z]' | cut -f2 -d: | cut -f2 -d' '"
if countrycode ≠ badcountrycode then exit repeat
toggleWifi("off", airPortDevice)
delay 6
toggleWifi("on", airPortDevice)
delay 2
end repeat

on toggleWifi(value, device)
        do shell script ("/usr/sbin/networksetup -setairportpower " & device & " " & value)
end toggleWifi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment