AppleScript: how restart automatically the internet connection when it's stuck

Viewed 34

When I upload a big amount of files through FTP for some reason my WiFi connection stops working. The connection doesn't show any problem and for solving this it's enough disable and re-enable the wifi. How to restart automatically it using AppleScript in the Script Editor?

1 Answers

My solution's this script that every second checks the connection to google and if there is a timeout close and reopen the en0 connection.

log "Internet checker: let's go"

repeat while true
    
    try
        do shell script "nc -w 4 -z 8.8.8.8 53"
        # do shell script "nc -w 4 -z apple.com 80"
        log "External TCP call test works"
        
    on error
        
        log "Restarting internet..."
        
        do shell script "networksetup -setairportpower Wi-Fi off"
        delay 2
        do shell script "networksetup -setairportpower Wi-Fi on"
        
        log "Done."
        
        delay 8
        
    end try
    
    delay 2 # wait a moment before the next internet checking
    
end repeat

Update 24 Aug 022: This should be my 'definitive edition' of the script: privileges are no more requested. It used to be pretty slow to discover the lack of internet connection despite the -w timeout, so I try to ask directly to a "standard de facto" IP (a DNS server).

For more information about nc command you can consult this link: https://www.computerhope.com/unix/nc.htm

Related