Resetting AirPort
December 26, 2010 at 11:31 AM by Dr. Drang
My recently retired iBook G4 sometimes had trouble finding my home wifi network when I opened the lid to wake it up. I suppose it would have found the network eventually, but I was never patient enough to wait. I’d go up to the AirPort menu, turn AirPort off, and turn it back on again.
This was a bit annoying, but I didn’t need to do it often enough to think about automating the process. Well, my new Macbook Air has this problem far more often, and because it’s so fast at waking up and launching applications, the failure to find the network right away is more frustrating. So I wrote a script to do the toggling with a keypress.
The script relies on the networksetup
command, a Mac-specific utility that seems to mimic the functionality of the Network preference pane in System Preferences. Here’s the script:
bash:
1: #!/bin/bash
2:
3: # Toggle AirPort off and on.
4: networksetup -setairportpower en0 off
5: sleep 2
6: networksetup -setairportpower en0 on
I call the script “Reset AirPort,” and I have it stored in ~/Library/Scripts
where FastScripts can find it. I’ve assigned it, via FastScripts, a keystroke shortcut of ⌃⌥⌘F12, so I can run it quickly whenever the MB Air fails to connect.
As you can probably guess, Line 4 turns AirPort off and Line 6 turns it back on again; Line 5 takes a two-second break in between. The only tricky thing is the en0
parameter in Lines 4 and 6. That’s the device name for the AirPort connection on my computer, assigned automatically by the system. The en
part is an abbreviation of Ethernet (these device names are an old Unix holdover from the days when your network connection always was an Ethernet cable) and the 0
indicates that it’s the computer’s first network connection.1 If I had two network connections, one would be en0
and the other would be en1
.
If you have two or more network connections and you want to find out which name is assigned to each, run
networksetup -listallhardwareports
The results on my MB Air are kind of dull:
Hardware Port: Bluetooth DUN
Device: Bluetooth-Modem
Ethernet Address: N/A
Hardware Port: AirPort
Device: en0
Ethernet Address: 11:9a:ed:b0:fc:f4
The Ethernet Address in the last line is usually called the MAC Address, a unique number for every network device in the world.2 Unlike an IP number, which changes when you move your computer from one network to another, the MAC address is “burned in” to the device itself—it’s assigned by the manufacturer according to some set of rules and never changes. If you’ve ever tried to limit access to your network by changing the router setup, you may have seen a form where you can enter MAC addresses to be filtered.
My guess is that after writing this script, I’ll no longer have any connection delays. It’s like how I keep the rain away by carrying an umbrella.
Update 12/26/10
Reader @potatowire told me about a free utility called SleepWatcher from Bernhard Baehr.
SleepWatcher 2.1 (running with Mac OS X 10.5 and 10.6, source code included) is a command line tool (daemon) for Mac OS X that monitors sleep, wakeup and idleness of a Mac. It can be used to execute a Unix command when the Mac or the display of the Mac goes to sleep mode or wakes up, after a given time without user interaction or when the user resumes activity after a break or when the power supply of a Mac notebook is attached or detached. It also can send the Mac to sleep mode or retrieve the time since last user activity. A little bit knowledge of the Unix command line is required to benefit from this software. For Mac OS X 10.3 and 10.4, use SleepWatcher 2.0.5 (and sources). For Mac OS X 10.1 and 10.2, download SleepWatcher 1.0.1. Awhile ago, it was a Pick of the Week at Mac OS X Hints.
Sounds like a program worth looking into.
-
If you don’t blink an eye when someone tells you the first item is called Item 0 instead of Item 1, you’ve spent too much time with computers. ↩
-
Just to show my paranoia, I’ve altered my MAC address in the output above. Yes, I know my MAC address is given out to every network I connect to; it just didn’t seem prudent to publish it in my blog. ↩