0

I am running an old windows 7 system on wifi that I remotely access to monitor.

The problem is that the wifi is unpredictable (due to odd router issues) and I need this system to be online at any given time of the day. Oddly, when the wifi drops, it does not tend to reconnect automatically even though it is still availble and windows is set to reconnect to it.

I found a similar question with an answer here but it was for a wired network and sadly, the solution doesn't work for my system.

It is a batch file as created below. I ran it, killed the wifi router which stopped the pings, but it did not complete the if statement to reset the interface. The command window simply shows the ping timed out and keeps repeating that until the connection is re-established. The log file does not log an error so it is never getting to the error portion of the if statement. I changed the original "Interface" var from "Local Area Connection" to "Wireless Network Connection"

Can anyone troubleshoot this or suggest an alternative option for resetting the adaptor automatically when the network drops as that seems to fix it. Do I need to add "Wireless Network Connection (%SSID%)" as the interface?

Thanks!

Panterateama

@echo off
set INTERFACE="Local Area Connection"
set TIMEOUT=3600
set IP=8.8.8.8
set LOG="watchdog.log"

echo %DATE% %TIME%: Watchdog started >> %LOG%

:loop
rem First check the interface for an hour by pinging the Google DNS
rem and resetting the networking interface if it should fail.
ping -n %TIMEOUT% -w 1000 -l 0 %IP%
if %errorlevel% NEQ 1 goto :loop
echo %DATE% %TIME%: Connection failed. Restarting interface.. >> %LOG%
netsh interface set interface %INTERFACE% disable
netsh interface set interface %INTERFACE% enable

rem Give it another shot but restart the whole computer if it the 
communication should still fail
ping -n %TIMEOUT% -w 1000 -l 0 %IP%
if %errorlevel% NEQ 1 goto :loop
echo %DATE% %TIME%: Still no connection. Restarting computer.. >> %LOG%
shutdown /r /c "Internet wathchdog"

echo %DATE% %TIME%: Waiting for system to shut down >> %LOG%
choice /T %TIMEOUT% /D Y /N > NUL
echo %DATE% %TIME%: Gave up on the shut down attempt. Trying again.. >> 
%LOG%
goto :loop

2 Answers 2

1

Change TIMEOUT to 30. This will tell it to perform the reset if the connection has been down for 30 seconds. Otherwise it has to be down for 3600 seconds (1 Hour).

Also, you should not use 8.8.8.8 for this purpose. Instead you should use your gateway IP address found with ipconfig. If you use an external IP like 8.8.8.8 your computer will reset your connection and even keep rebooting your computer if something like your internet simply goes down.

By using an internal IP like your gateway address, it will only reset or reboot when the wifi connection is actually down.

Also, you can run this as a scheduled task to run with highest privileges as nt authority\system on computer startup. But, I would change the timeout to something a little longer, like 300 seconds because the script might run before your network stack is fully operational.

EDIT: As stated in the comments, the script posted in the question simply doesn't work. The problem with the original script is that the ping command might return a "Reply from xxx.xxx.xxx.xxx: Destination host unreachable" which results in a "successful" error level.

Below is an updated script that works, and changes the logic so that it more effectively detects a downed connection. I also removed the part that would reboot the computer - I feel that is unnecessary and potentially problematic. Note that there will be NO output on the screen from this command when it is running. Make sure the proper network interface name has been obtained using the netsh interface show interface command.

@echo off
REM Modified by Appleoddity - 10/11/2017
REM Fixed ping status check; Removed shutdown/reboot ability; Improved logging
REM This script MUST RUN AS ADMIN
REM
REM Obtain network interface name with 'netsh interface show interface'
REM 
REM Change the INTERFACE; THRESHOLD; IP; and LOG variables below.
REM Note: THRESHOLD is not necessarily the number of seconds before reset - but close
REM
setlocal EnableDelayedExpansion
set INTERFACE="Wi-Fi"
set THRESHOLD=30
set IP=10.1.10.1
set LOG="watchdog.log"

echo %DATE% %TIME%: Watchdog started >> %LOG%

SET /A COUNT=0
REM Loop until <THRESHOLD> failed, consecutive pings are counted
:LOOP
ping -n 1 -w 1000 -l 0 %IP% | find /i " bytes=" >NUL 2>&1
if errorlevel 1 (
    set /A COUNT+=1
    echo %DATE% %TIME%: Failed ping detected - Count = !COUNT!. >> %LOG%
) ELSE (
    set /A COUNT=0
    REM Pause
    ping -n 2 -w 1000 -l 0 127.0.0.1 >NUL 2>&1
)
if %COUNT% GEQ %THRESHOLD%  (
    echo %DATE% %TIME%: %THRESHOLD% failed pings exceeded. >> %LOG%
    GOTO RESET
)
GOTO LOOP

REM Reset the network interface
:RESET
echo %DATE% %TIME%: Restarting network interface - %INTERFACE% >> %LOG%
netsh interface set interface "%INTERFACE%" admin=DISABLED >NUL 2>&1
netsh interface set interface "%INTERFACE%" admin=ENABLED >NUL 2>&1
SET /A COUNT=0
GOTO LOOP
7
  • I will try this tomorrow and kill the network to see the results, I didn't even think about the delay being so long! And using the gateway makes a lot more sense, the last thing I need if for my ISP to go down and the system rebooting a hundred times for nothing. Thanks! Oct 11, 2017 at 0:51
  • So I tested the suggested changes and it went down again. I went to find the system offline again, set the watchdog bat to 30 seconds timeout and the gateway address and it still did not reset or trigger the if command. It fluctuates between timed out and destination unreachable and I wonder if that is why... it is not a solid ping failed for 30 seconds... Oct 11, 2017 at 5:24
  • The script is different from anything I've used. This is not how I would do it, and I suspect the script simply doesn't work and I'm pretty sure I know why. I'll take a look at post back later. Oct 11, 2017 at 11:36
  • @panterateama I've updated my answer to include a new script that works. See the notes. Oct 11, 2017 at 14:33
  • Awesome! This makes more sense to me! if it doesnt find the specific success message, then fail. I like it! I will test it tonight when I get home, Thanks! Oct 11, 2017 at 16:51
0

If you are getting "An interface with that name is not registered with the router" error, then it means you are not selecting the correct network adapter in your script.

Are you running the batch/command as Admin?

If you want to make sure you have admin rights, you can run ncpa.cpl or

netsh interface show interface

which gives you a list of adapters and then write something like this, based on your adapter names:

netsh interface set interface name="Local area connection" admin="disabled"
netsh interface set interface name="Wireless Network Connection" admin="disabled"
netsh interface set interface name="Local area connection" admin="enabled"
netsh interface set interface name="Wireless Network Connection" admin="enabled"

This will reset the adapter you require, plus you can use & to string them all together:

netsh interface set interface name="Local area connection" admin="disabled" & netsh interface set interface name="Local area connection" admin="enabled"
1
  • 1
    Thanks for the suggestions, I did run the command to get the interface name and I will string them together in the final file with admin="" Oct 11, 2017 at 0:52

You must log in to answer this question.

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