Saturday, April 19, 2014

Inet Keeper - Keeps you connected to the Internet

[Post available in English, Español & Português]
I'm in Brazil now, and here I have this 3G Internet connection trough a SIM mobile phone chip inserted in a 3G modem, connected via usb to the computer. This 6 KBps connection is not the most stable thing in the world, so it disconnects sometimes.

I can't stream videos, but I can download them (trough hours) and watch later with any player. If Internet disconnects and I'm on the computer, is easy to reconnect it (for some reason, the "auto reconnect" from windows doesn't work, even after format), but if it's 1am, I leave downloading the complete series of Bravest Warriors, an application, a Coursera video lesson, a manga, or anything else, I'm sleeping, and Internet disconnects, I'll loose 8 hours of downloads that I'll need to do now during the day, slowing down everything else, etc.

Because of that, I make this script. It's really simple: a .bat that checks if you're connected to the Internet (ping google), if not, tries to connect (using rasdial), if you're connected, waits a minute or so, checks again and repeat, for all eternity.

Inet Keeper screenshot
The only things to configure are:
  1. First two words of a successful ping reply (it depends on your Windows language):
    --- English: "Reply from"
    --- Portuguese: "Resposta de"
    --- Spanish: "Respuesta de"
  2. Name of the connection (in this case, "Oi 3G").
  3. The history directory
  4. And if you want to check faster or slower, the time between checks, in seconds.

Here is the code:
@ECHO OFF

::VARIABLES


::First two words of a (succeful) ping response

SET _ping_reply=Resposta de

::Name of the internet connection

SET _conn_name=Oi 3G

::Time between checks (in seconds)

SET _time=60

::Directory to save history

SET _hist_dir=D:\ik_hist.txt

TITLE Inet Keeper


ECHO ---------------------------------------------------

ECHO  Inet Keeper - Keeps you connected to the Internet
ECHO ------------------------------------- by JackS ----
ECHO.
ECHO - Ping reply start: "%_ping_reply%"
ECHO - Internet connection name: "%_conn_name%"
ECHO - History directory: "%_hist_dir%"
ECHO - Time between checks: %_time% seconds
ECHO.
ECHO -------------------------------------------

GOTO :CHECKCONNECTION


:CHECKCONNECTION

ECHO  @ @ ^| Checking connection, please wait...
PING -n 1 www.google.com|find "%_ping_reply%" >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF     ERRORLEVEL 1 goto :TRYRECONNECT

:SUCCESS

ECHO   U  ^| You are connected to the internet.
ECHO  \_/ ^| Waiting %_time% seconds to check again...
PING -n %_time% 127.0.0.1>nul
ECHO -----+-------------------------------------
GOTO :CHECKCONNECTION

:TRYRECONNECT

ECHO ----- >> "%_hist_dir%"
date /T >> "%_hist_dir%"
time /T >> "%_hist_dir%"
ECHO Ping failure. Trying to reconnect. >> "%_hist_dir%"
ECHO   U  ^| Ping failure!
ECHO  ~~~ ^| Trying to reconnect to %_conn_name%...
RASDIAL "%_conn_name%" >> "%_hist_dir%"
ECHO -----+-------------------------------------
PING -n 5 127.0.0.1>nul
GOTO :CHECKCONNECTION
http://pastebin.com/4cPi6YBa

Just copy the code in a .txt file, save it and rename it to "something.bat".
And yes, you've to let that little black window open. Always.

Hope this was helpful for you as it was for me, at least to see how to code another random thing in this shitty language.

No comments:

Post a Comment