Reboot Router Automatically when the Internet Disconnects

So; if you’re going to be going away for 3+ weeks and need your Internet to stay up.  What if the router needs a reboot or it goes haywire?  Needless to say you can’t do it remotely if the Internet is unavailable (actually, there are some cool power-sockets that can cycle the power when they get an SMS text to an installed SIM card).

Here’s a solution I came up with;

First, the Energenie Power Management System.  It’s basically a multi-block power socket with a LAN connection that allows you to cycle each power socket according to criteria.  The inbuilt software allows it to cycle on a schedule or on the presence of a file etc.

What it ALSO includes is a command-line app that allows you to cycle the plug sockets.

So add a little Powershell magic;

$PauseTime=10
do
{
    $FailCount=0
    $TryCount=0
    do
    {
        $InternetOn=$False
        foreach($Server in $TestServers)
        {
            $InternetOn=($InternetOn -or (Test-Connection $Server -Quiet))
        }
        if(-not ($InternetOn))
        {
            $FailCount=$FailCount+1
            $Message="Internet failed.  Failure count : "+ $FailCount
            Write-EventLog -logname Application -source "Internet Checker" -Message $Message -EventId 101 -EntryType Warning -Category 1
        }else
        {
           $FailCount=0
        }
        Start-Sleep -Seconds $PauseTime
        $TryCount=$TryCount+1
        if($TryCount -ge 20)
        {
            Write-EventLog -logname Application -source "Internet Checker" -Message "Internet Functioning Ok" -EventId 101 -EntryType Information -Category 1
            $TryCount=0
        }
    }while($FailCount -lt $FailThreshold)
    Write-EventLog -logname Application -source "Internet Checker" -Message "Internet Failed, turning off power to Router." -EventId 101 -EntryType Warning -Category 1
    & "C:\Program Files\Power Manager\pm" -off -PLAN -Socket1
    Start-Sleep $PauseTime
    Write-EventLog -logname Application -source "Internet Checker" -Message "Restarting power to Router." -EventId 101 -EntryType Information -Category 1
    & "C:\Program Files\Power Manager\pm" -on -PLAN -Socket1
}
while($true)
Write-EventLog -logname Application -source "Internet Checker" -Message "Checker Ended" -EventId 101 -EntryType Information -Category 1

Start the script automatically when the system boots and you’re set!

“& “C:\Program Files\Power Manager\pm” -off -PLAN -Socket1″ is the part that runs the commandline utility.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s