Jake.codes

Reconnect WiFi

Saturday, July 20, 2013

My WiFi kept disconnecting on my Mac for a period, so I made this to double check for me while I was away.

WIFI_SSID = "YOUR WIFI NETWORK"
WIFI_PASS = "YOUR WIFI PASSWORD"

def checkWiFi
    currentSSID = %x{networksetup -getairportnetwork en0 | awk '{gsub(\"Current Wi-Fi Network: \",\"\");print}'}
    if currentSSID.strip.eql?(WIFI_SSID)
        return true
    else
        reconnectWiFi
    end
end

def reconnectWiFi
    %x{networksetup -setairportnetwork en0 "#{WIFI_SSID}" "#{WIFI_PASS}"}
end

while true do
    checkWiFi
    sleep 3
end