Script to toggle WiFi from keyboard for OS X
Now that I have a machine (Macbook Air) with decent battery life, I decided to take it a step further and see if I could extend it by staying offline as much as possible. This is an even better idea considering how much faster my Air attaches to the network. So I wrote a little bash script that toggles the wifi power state. I activate it using Quicksilver and have setup the script to be the default result for "w". If you want to use it, just make sure to replace the line NET_DEVICE="en0"
with whatever label your WiFi device is given. For example, it is en1
on my old Macbook because en0
is the wired connection.
#!/bin/bash
NET_DEVICE="en0"
WIFI_STATE=`ifconfig $NET_DEVICE | grep status | cut -d ' ' -f2`
if [ $WIFI_STATE = "inactive" ]
then
echo "$NET_DEVICE is $WIFI_STATE, enabling"
networksetup -setairportpower $NET_DEVICE on
else
if [ $WIFI_STATE = "active" ]
then
echo "$NET_DEVICE is $WIFI_STATE, disabling"
networksetup -setairportpower $NET_DEVICE off
fi
fi