ping test


can u help a simple code that runs a ping test to 3 different servers and write these results to a log file? we dont want it to append each result to the log file

all help will do
Who is Participating?
Let's assume that the servers are named Server1, Server2, and Server3, c:\log exists, and that name resolution is working.

The following should work:

ping server1 > c:\log\server1ping.txt
ping server2 > c:\log\server2ping.txt
ping server3 > c:\log\server3ping.txt
Something like this? It'll open one command prompt for each server, will only log status changes, and (the most important feature) change the window color according to the current status.
ServerList can be host names or IP addresses; the log files will be created in the script's folder, begin with the script's name, then the pinged name.
@echo off
set ServerList=
if "%~1"=="" (
	for %%a in (%ServerList%) do (
		start "" "%~f0" %%a
	goto :eof
set IP=%~1
mode con cols=40 lines=25
title Ping %IP%
set LogFile=%~dpn0-%IP%.log
set Lost=X
ping.exe -4 -n 1 %IP% | find /i "TTL" >NUL
if errorlevel 1 (
	if not %Lost%==1 (
		color 4F
		echo - Down: %Date% %Time: =0%
		>>"%LogFile%" echo - %Date% %Time%
		set Lost=1
) else (
	if not %Lost%==0 (
		color 2F
		echo + Up: %Date% %Time: =0%
		>>"%LogFile%" echo + %Date% %Time%
		set Lost=0
	ping.exe -4 -n 2 localhost >nul
goto :loop

Open in new window

rutgermonsAuthor Commented:
thanks comprobsolv

how can i get it only right to one file and append and not overwrite
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Hi rutgermons...

Per CompProbSolv's solution, change all to:
>> c:\log\pingresults.txt

Open in new window

Basically, use >> instead of >
Steve KnightIT ConsultancyCommented:
Glad I didn't think more complicated too oBdA!

NVIT's answer is correct if you want to keep ALL old logs.  If you want to start a new log (erasing the old one) each time you run the set of 3 tests, use > on the first one and >> on the second and third.  You could do some rename commands at the start to keep some number of old logs.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.