Edward Loveall

Automatically Lock Your Computer Based on Wi-Fi

I use my computer in multiple environments, at home, the office, and out and about. I like having the password prompt after sleep or screensaver start. But I do not like having to enter my password all the time.

Solution: Enable password protection when not at home.

Problem: That’s annoying to remember.

Solution: Don’t.

I wrote a script to detect your what Wi-Fi network you’re on. If it’s not your home network, it sets the password lock. If it is your home network, disable it. This runs via a launchd plist every five minutes (adjustable) and is pretty dang fast.

Note: this only works on a Mac.

Installation

Now every five minutes (that 300 in the plist) it should run the autolock.sh script and set or unset the password protection. To make sure it’s working, change the SSID name to something else, unload and load the launchd job again and check in System Preferences under Security. “Require password X seconds after sleep or screensaver begins” should reflect the change, one way or another.

Bonus

I thought I was going to need a nice sanitized name for the current Wi-Fi network name, but the way this script works, it doesn’t really end up needing it. It just needs to know if you’re on that network or “something else”. If you do need that functionality, here are some ways to get it:

$ ioreg -rn en0 | grep -e '[^B]SSID' | ruby -e 'puts ARGF.read.chomp.split(/"/).last'

$ ioreg -rn en0 | grep -e '[^B]SSID' | awk 'BEGIN { FS = "\"" }; { print $4 }'

$ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep -e '[^B]SSID' | awk 'BEGIN { FS = ": " }; { print $2 }'

Update

Gabe fixed up my awk code for me so the last two commands there can be:


$ ioreg -rn en0 | awk 'BEGIN { FS = "\"" }; /[^B]SSID/ { print $4 }'

$ /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk 'BEGIN { FS = ": " }; /[^B]SSID/ { print $2 }'