Skip to content

Instantly share code, notes, and snippets.

@pix0r
Last active May 25, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pix0r/ec0f5b7e8341b61d8426f70a060a4ea5 to your computer and use it in GitHub Desktop.
Save pix0r/ec0f5b7e8341b61d8426f70a060a4ea5 to your computer and use it in GitHub Desktop.
#!/bin/sh
device="en0"
IFS=$'\n'
networks=`networksetup -listpreferredwirelessnetworks $device | tail -n+2 | sed -e 's/^[[:space:]]*//'`
function usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo "\t-h\t\tPrint this message"
echo "\t-l\t\tList networks"
echo "\t-d <network>\tDelete specified network"
echo "\t-i\t\tInteractively review and delete networks"
echo "\t-e\t\tLaunch editor to select networks to delete"
}
function interactive() {
for network in $networks; do
printf -- "Remove network '$network'? y/N "
read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
delete $network
fi
done
}
function editor() {
tmp=`mktemp`
sep="---------------------------------------------------------------"
echo $sep >> $tmp
echo "---- Upon saving this file and exiting the editor, all networks" >> $tmp
echo "---- in the list below will be deleted:" >> $tmp
echo $sep >> $tmp
echo "$networks" >> $tmp
# Edit network list
"${EDITOR:-vi}" $tmp
remove=`cat $tmp|grep -v ^----`
lines=`echo "$remove" | wc -l`
if [ "$remove" = "" ]; then
echo "No networks to remove"
else
echo "$sep"
echo "Networks to remove:"
echo "$sep"
echo "$remove"
echo "$sep"
printf -- "Are you sure you want to remove $lines networks? y/N "
read ans
if [ "$ans" = "y" -o "$ans" = "Y" ]; then
for line in `cat $tmp|grep -v ^----`; do
delete $line
done
else
echo "Not removing networks"
fi
fi
rm $tmp
}
function delete() {
network=$1
if [ "$network" = "" ]; then
usage
exit 1
fi
networksetup -removepreferredwirelessnetwork $device "$network" && echo "Removed." || echo "Error removing"
}
function list() {
echo "$networks"
}
case $1 in
-l)
list
;;
-d)
delete $2
;;
-h)
usage
;;
-i)
interactive
;;
-e)
editor
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment