Toggling wi-fi on and off

DISCLAMER: I can't check this (that it actually works) at the moment, but I will later.

This script to turn on/off wifi is taken from here:

try
    set makiaeawirelessstatus to do shell script "networksetup -getairportpower en1"
on error
    display dialog "The script did not work as intended, please check the networksetup command (in terminal) works on your system. It has been tested on mac os 10.7 (Build 11A459e). Other versions of mac os may not have this command available. Please open the applescript in applescript editor for more details." buttons {"kthxbai"}
end try

if makiaeawirelessstatus is "Wi-Fi Power (en1): On" then
    do shell script "networksetup -setairportpower en1 off"
else if makiaeawirelessstatus is "Wi-Fi Power (en1): Off" then
    do shell script "networksetup -setairportpower en1 on"
else
    display dialog "The script did not work as intended, please check your wireless connection is specified correctly. The default in this script is en1 (please open the applescript in applescript editor for more details)" buttons {"kthxbai"}
end if

You'd need to edit it to make it accept arguments (I would, but it'd turn out something like javascript)

Then, if you want the script to run reaccuringly, use something like LaunchAgent or cron. I prefer LaunchAgent, because it's more forgiving.


Edit the root's crontab (by for example running EDITOR=nano sudo crontab -e) and add a line

*/15 * * * * ifconfig en1 down;ifconfig en1 up

where en1 is the identifier shown by networksetup -listallhardwareports|awk '/^Hardware Port: (Wi-Fi|Airport)/{getline;print $2}'.

The ifconfig commands require superuser privileges.