Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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:

repeat
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.

share|improve this question
    
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

2 Answers 2

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.
    try
        set pingResult1 to do shell script "ping -c 1 208.67.222.222"
    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.
        try
            set pingResult2 to do shell script "ping -c 1 208.67.220.2220"
        on error
            set pingResult2 to ""
        end try

        set p to number of paragraphs in pingResult2
        if p < 5 then return false
    else
        return true
    end if
end checkInternetConnection
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.