Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Files - Using ping to test network connectivity

Using a batch file would it be possible to do something like:

ping google.com

if return success do ECHO You are connected to the internet

else return ECHO You are not connected to the internet

like image 572
Peter Avatar asked Jun 15 '11 12:06

Peter


2 Answers

You can use following snippet:

@echo off
Ping www.google.de -n 1 -w 1000
if errorlevel 1 echo Not connected
like image 153
CSchulz Avatar answered Oct 01 '22 20:10

CSchulz


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
like image 28
jfgoodhew1 Avatar answered Oct 01 '22 18:10

jfgoodhew1