TOGGLE WIFI ON/OFF

Nice little script to toggle WiFi on or off on a Mac. I have had a lot of issues with 2016/2017 Macbook Pros dropping WiFi connectivity. Solving it involves toggling, forgettng saved SSID creds or fully restarting the machine, so I put together a bunch of scripts to do it all.

QUICK TOGGLE

Use this to toggle WiFi on or off.

#!/bin/bash

wifiAdapter=$(/usr/sbin/networksetup -listallhardwareports | egrep -A 2 "Hardware Port: (Air|Wi-)" | grep "Device:" | awk '{print $2}')
wifiPower=$(/usr/sbin/networksetup -getairportpower "$wifiAdapter" | awk '{print $NF}')
if [[ "$wifiPower" = "On" ]]; then
    echo "Toggling Wi-Fi adapter..."
    /usr/sbin/networksetup -setairportpower "$wifiAdapter" off
    sleep 5
    /usr/sbin/networksetup -setairportpower "$wifiAdapter" on
fi

exit

FORGET SSID

Quick script to forget SSID
#!/bin/sh

networksetup -removepreferredwirelessnetwork $interface YourNetworkSSIDHere