Batch File to Maintain Wireless Connection

This is a Windows batch file and a hack for working around intermittent wireless disconnects. If your connection periodically goes to “no internet access”, this script may help. Obviously, it would be better to find and fix the problem but this may keep you going until you do.

This seems so easy that there must be something wrong with it, but I don’t know what that is yet, so I don’t promise anything. It works for me and I can even keep a putty session going with this script running despite frequent intermittent disconnects.

My script figures out the default gateway and the SSID. It pings the default gateway and upon failure, it will reconnect using the SSID. Then it waits 30 seconds and starts over.

I came up with this script after finding plenty of scripts that were pretty close but not quite what I wanted. Some required that you figure out the default gateway and/or the ssid before running the script. Some required the name of the interface. I wanted something that didn’t require my input.

This will open a command window. The command window reports when a reconnection was required and also allows you to hit a key to bypass the 30 second wait period for one round.


@setlocal enableextensions enabledelayedexpansion
@echo off
:loop
for /f "tokens=3 delims= " %%a in ('netsh wlan show interfaces ^| findstr "^....SSID"') do (
set ssid=%%a
)
for /f "tokens=13 delims= " %%a in ('ipconfig ^| findstr "Default.Gateway.*[0-9]"' ) do (
set gateway=%%a
)
ping -n 1 %gateway% | find "TTL="
if errorlevel 1 (
goto :reset
) else (
@timeout /t 30
goto :loop
)
:reset
time /T
netsh wlan connect %ssid%
@timeout /t 60
goto :loop
endlocal

Advertisements
Leave a comment

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