Sometimes I need to download very big files from the internet using torrents (I'm using Transmission) and the Wi-Fi always drops. I bought several routers and modems and none of them work. I've been having this issue a for a while (~10 years, with both Windows and Mac).

Now I decided to learn a bit, and write some AppleScript which reconnects automatically:

if (do shell script "curl --connect-timeout 1 <WIFI_IP>") = "curl: (7) couldn't connect to host" then
    do shell script "networksetup -setairportpower AirPort off"
    do shell script "networksetup -setairportpower AirPort on"
    do shell script "networksetup -setairportnetwork en1 <ROUTER_NAME> <ROUTER_PASS>"
end if
end repeat

the part after then works, it disconnects from Wi-Fi and reconnects it, but the problem is with the if part, because I don't know how to detect when the Wi-Fi connection is broken by the torrent. How should I do this?

Also, a repeat with an interval would be really nice, something like: repeat every 10 seconds.

  • I'd add a delay like delay 300 just before the repeat end to keep the script from constantly checking for a connection. 300 seconds would have it check every 5 mins.
    – Vickash
    Jan 3 '12 at 19:45

Here's a simple Appplescript handler I wrote a while back to solve this exact problem. It pings the OpenDNS server (both of them for redundancy) to check if the internet connection is up. It returns true on success, false on failure. Just copy it into your script, and call it like:

set connected to checkInternetConnection

Then you can put whatever you want to do in an if statement like:

if connected then
    -- Do stuff here
end if

The handler:

on checkInternetConnection()
    -- Ping the primary OpenDNS server.
        set pingResult1 to do shell script "ping -c 1"
    on error
        set pingResult1 to ""
    end try

    -- Check the results returned and return true or false.
    set p to number of paragraphs in pingResult1
    if p < 5 then
        -- Ping another Open DNS server for redundancy.
            set pingResult2 to do shell script "ping -c 1"
        on error
            set pingResult2 to ""
        end try

        set p to number of paragraphs in pingResult2
        if p < 5 then return false
        return true
    end if
end checkInternetConnection
  • returns error Can’t make «handler checkInternetConnection» into type boolean
    – ljs.dev
    Feb 18 '15 at 0:16

At home, I maintain a perl script called ping-random-server. Whenever I suspect a networking problem, I run it. ping is an option for you as well. Run a ping command with a limited number of pings against an ip address or hostname known to respond to ping, and take action if the ping returns 100% packet loss.

As to how to fit this into your applescript, try

if (do shell script "ping -c 10 www.whitehouse.gov | grep transmitted") = "10 packets transmitted, 0 packets received, 100.0% packet loss"

I haven't tested this other than by running the ping commands in Terminal and examining the output.

You must log in to answer this question.

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