Skip to content

Instantly share code, notes, and snippets.

@FiloSottile
Created April 5, 2017 16:02
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save FiloSottile/70bac8e4cda11c769316a9aabf2cd1f2 to your computer and use it in GitHub Desktop.
Save FiloSottile/70bac8e4cda11c769316a9aabf2cd1f2 to your computer and use it in GitHub Desktop.
#! /bin/bash
set -euo pipefail
# This script will remove automatic association for all networks not listed in the whitelist
# passed as the first argument. Passwords will NOT be removed from the Keychain.
#
# Alternatively, you can untick "Remember networks" in Network Preferences > Wi-Fi > Advanced,
# but then you won't be able to auto-join networks even temporarily, and you might already
# have a long list to go through.
#
# Having automatic association for open or known-password networks is dangerous as it
# allows an attacker to force you on their network by simple proximity.
#
# You'll have to run this as sudo not to be prompted at every entry.
DEVICE="en0"
networksetup -listpreferredwirelessnetworks "$DEVICE" | tail -n +2 | cut -d$'\t' -f2- | \
while IFS= read -r network
do
grep -Fxe "$network" "$1" > /dev/null || (
networksetup -removepreferredwirelessnetwork "$DEVICE" "$network"
)
done
@prenagha
Copy link

prenagha commented Apr 5, 2017

for other clueless people like me... the script requires an argument
the argument should be the path to the whitelist file
the whitelist file is one network name per line, those networks you want this script to skip
any network not whitelisted will be removed from the WIFI preferred network list

this works great. thanks.

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