Batch Files - Using ping to test network connectivity

You can use following snippet:

@echo off
Ping www.google.de -n 1 -w 1000
if errorlevel 1 echo Not connected

Here is a script to help you start with it:

http://www.techimo.com/forum/networking-internet/73769-handy-batch-file-check-network-connectivity.html

NOTE: If your system is not in English, you will have to modify the lines in the script where find command is being used to filter Reply from from the ping's output to the corresponding string in the system's language.


@echo off
echo Checking connection
ping -n 1 www.google.com >nul
if errorlevel 1 (
  cls
  echo Failed
  pause>nul
  exit
)

cls
echo Success!
pause>nul
exit

Here's a script that will repeatedly check, and write the time (from system clock) and "internet offline" to a log file at C:\Internet.txt each time the internet goes offline. Unfortunately the latest line in the log file will appear at the end - I don't know how to make it appear at the top ;)

BTW: I set the wait time (-w) to 20 seconds, because I was using a 3G dongle (with 2G internet) so 20s was often the only way to be sure if the internet was really down or something else was the problem... Feel free to change it to 5000 for 5s, or delete "-w 20000" altogether to leave it at default.

@echo off

:START

ping -n 4 4.2.2.2 -w 20000 >nul

if %errorlevel% == 1 (
  echo Internet offline >> C:\Internet.txt
  Time /t >> C:\Internet.txt
)

Timeout /t 30
@set errorlevel = 0

GOTO START