Skip to content

Instantly share code, notes, and snippets.

@jsleeio
Last active January 7, 2016 11:48
Show Gist options
  • Save jsleeio/dcb71eef6cd0f2632c83 to your computer and use it in GitHub Desktop.
Save jsleeio/dcb71eef6cd0f2632c83 to your computer and use it in GitHub Desktop.
#!/bin/bash
# John Slee <indigoid@gmail.com>
keep_until="heart of gold"
get_wifi_interface() {
networksetup -listallhardwareports | sed -n '/Wi-Fi$/,/^Device/ { s/.*\(en[0-9]\)$/\1/p; }'
}
get_wifi_ssids() {
IFS="\\012" networksetup -listpreferredwirelessnetworks $1 \
| sed -n "1,/$2/d; s/^[[:space:]]*//; p"
}
if=$(get_wifi_interface | head -1)
# quick sanity check
if ! /sbin/ifconfig $if > /dev/null 2>&1 ; then
echo "can't find your wifi, halp"
exit 1
fi
if [ "$1" == "no-really" ] ; then
really=1
else
echo "Dry-run. Pass 'no-really' on the commandline to actually remove SSIDs."
echo
fi
get_wifi_ssids "$if" "$keep_until" | while read ssid ; do
if [ "$really" == 1 ] ; then
sudo networksetup -removepreferredwirelessnetwork "$if" "$ssid"
else
echo "Found unwanted SSID: $ssid"
fi
done
@yaleman
Copy link

yaleman commented Jan 7, 2016

Probably need to check that en0 is your wireless interface (mine's en1) :) It'll just have a whine if it's not.

yaleman$ networksetup -listpreferredwirelessnetworks en0
en0 is not a Wi-Fi interface.

@jsleeio
Copy link
Author

jsleeio commented Jan 7, 2016

Fixed!

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