If a wifi network disappears (maybe out of range or losing power) then this MacBookPro8,1 (Mojave 10.14.6) won't auto rejoin unless I click it again manually or toggle wifi on and off.
Yes the network is set to Auto Join.
I use this script as a cheap workaround, but wondering if I can address the issue directly.
Yes the network is set to Auto Join.
I use this script as a cheap workaround, but wondering if I can address the issue directly.
Code:
#!/bin/bash
# Interval to check and toggle Wi-Fi (in seconds)
INTERVAL=30
# Get the correct network interface name for Wi-Fi
WIFI_INTERFACE=$(networksetup -listallhardwareports | grep -A 1 "Wi-Fi" | tail -n 1 | awk '{print $NF}')
# Function to check Wi-Fi status and toggle if necessary
toggle_wifi_if_needed() {
# Get the Wi-Fi status (on/off)
WIFI_STATUS=$(networksetup -getairportpower "$WIFI_INTERFACE" | awk '{print $4}')
# Check if Wi-Fi is on
if [[ "$WIFI_STATUS" == "On" ]]; then
# Check if Wi-Fi is connected to a network
SSID=$(networksetup -getairportnetwork "$WIFI_INTERFACE" 2>&1)
# Handle the case where Wi-Fi is not connected
if [[ "$SSID" == *"You are not associated with an AirPort network."* ]]; then
echo "$(date): Wi-Fi is on but not connected. Toggling Wi-Fi..."
# Turn Wi-Fi off
networksetup -setairportpower "$WIFI_INTERFACE" off
sleep 5
# Turn Wi-Fi back on
networksetup -setairportpower "$WIFI_INTERFACE" on
else
# Extract and print the SSID
CONNECTED_SSID=$(echo "$SSID" | awk -F': ' '{print $2}')
echo "$(date): Wi-Fi is connected to network: $CONNECTED_SSID"
fi
else
echo "$(date): Wi-Fi is off. No action needed."
fi
}
# Main loop
while true; do
toggle_wifi_if_needed
sleep $INTERVAL
done