Tuesday, 20 September 2005

Batch File to Fix my Internet

The setup of the computers at my flat to access the Internet is like this:

Internet--USB ADSL Modem--Server--Fast Switch--Access Point--Computers
                                             |
                                             |-Computers
Some computers are connected to the server via the router, others by the wireless router. Everything is happy in the networking world, except for one problem.

Sometimes the modem on the server drops the Internet connection, and cannot reconnect. The only way that I could find to fix the problem was to logon to the Server with VNC and restart the computer. Eventually I got sick of doing this (after the second time) and decided to write a batch file to do the job for me (after the millionth time).

The batch file checks every 15s to see if the Internet is still working by pinging Google.com. I did start with Microsoft.com, but they must have some automatic protection, because after about 10min of pinging, all further requests were bounced. If the ping fails consistently after three attempts at the ping command, the computer reboots.

The batch file on the server is like so:

@echo off
set timeouts=
:start
echo Testing Internet Connection...
set timeout=
for /f "tokens=1*" %%r in ( 'ping -n 1 google.com' ) do (
  if "%%r"=="Unknown" (
    set timeout=o
  )
  if "%%r"=="Request" (
    set timeout=o
  )
)

if "!timeout!"=="o" (
  echo Failure
  set timeouts=!timeouts!c
) else (
  echo Success
  set timeouts=
)

if "!timeouts!"=="ccc" (
  echo About to restart computer
  net send vincentp4 The Internet computer is about to restart in 20+10s.
)

choice /Ty,20 Continue? 

if errorlevel 2 goto end

if "!timeouts!"=="ccc" (
  net send vincentp4 The Internet computer is going down now!
  shutdown /r /t:10 "Shutting down to fix Internet!" /c
  echo Command issued
  goto end
)

goto start
:end
This file is stored at "c:\bat\test.bat" the machine has Windows 2000 installed. The bat directory also includes choice.com and shutdown.exe. In my start menu startup folder I have the following batch file:
net send vincentp4 This computer has restarted and the Internet needs to be reconnected.
"C:\Program Files\Alcatel\SpeedTouch USB\STDialUp.exe" /WindowsDial /Entry "Alcatel Speedtouch Connection"
net send vincentp4 The Internet is now connected.
cd c:\battest
Which sends me a message before it dials, and after it dials, then starts test.bat.

If you need any explanation, just leave me a comment and I will elaborate.

No comments: