I'm using Mac 10.9.5. When I'm at a Starbucks, my computer will automatically connect to their WiFi network. However, after an hour, they kick you off and although you are still connected to their network, beneath your "WiFi" icon in your Network System preferences, you now have a "Self Assigned IP" and are unable to connect to the Internet. The only way to reconnect is to click the "Turn Wi-Fi Off" button and then click "Turn Wi-Fi On" button to re-enable things.

My question is, how do I write a script to automatically do this for me? What I woudl like is some script run in the background if I'm connected to a network named "Google Starbucks" and then if the script notcied I have a "Self Assigned IP", it woudl turn wi-fi off and turn it back on again without my having to do anything.

  • There's not much difference having to toggle the on/off/on state of the Wi-Fi from the Apple menu vs. doing it programmatically since you're still having to at least click on something or enter a command in a Terminal. That said I can give you an simple AppleScript or BASH script that can toggle the on/off/on state of the Wi-Fi if you want it. Coding it to check and wait etc. is doable but I don't have the time to mess with it. Mar 16, 2015 at 22:05
  • Yes I am very grateful if you have a script that can toggle on/off state of the WiFi. Thanks,
    – Dave
    Mar 16, 2015 at 22:25

3 Answers 3


In AppleScript use: do shell script "networksetup -setairportpower en0 off; sleep 2; networksetup -setairportpower en0 on" although you might have to change en0 to the appropriate adapter which you can get from the Network Utility.

If you want it in a BASH script don't use the do shell script part and the rest can be on one line or remove the ; adding a newline in there place. You could swap the display dialog ... in Buscar웃's script with this if you want.


Here is the part 1 of the script.

It will monitor if you are connected to the Internet and alert you when you are not.

        do shell script "ping -c 5 google.com"
    on error
        display dialog "You are not connected to the internet"
    end try
end repeat

Now you can cycle the WiFi off/on (honestly that is easy to do) or if you must you can write the second part of the script.

  • THanks. Because I don't want the cycling on and off to occur when I'm at home or on other networks, do you know how I can work in the network name "Google Starbucks" into the equation as well as the fact that I only want to activate the cycling when I have a self-assigned IP?
    – Dave
    Mar 16, 2015 at 22:24
  • This one does not do any cycling. Only checks if you are connected to the internet. As long you are it does nothing. If you are not connected it will tell you that. No need to change google.com to something else but you can.
    – Ruskes
    Mar 16, 2015 at 23:47

How about an Applescript app. Running every 10 seconds. That checks the IP and checks the SSID

    on idle
            set checkSSID to (do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I |grep ' SSID:'|awk -F:\\  '{print  $2}' ")

            set checkIp to do shell script "ifconfig en0|grep 'inet '|cut -d ' ' -f 2"

            if checkSSID starts with "Google Starbucks" and checkIp starts with 169 then

                do shell script "networksetup -setairportpower en0 off; sleep 2; networksetup -setairportpower en0 on"
            end if
            return 10 -- seconds
end idle

Save the Applescript as a stay open Application.

The app will then fire every 10 seconds. Or whatever you set return 10 to

afaik "Self Assigned IP" normally starts with 169. But change it to what you are getting.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .