When managing multiple servers, it’s essential to monitor their network availability regularly. Instead of checking each server manually, you can automate this task using a simple Windows Batch script that pings each server and logs the results into a CSV file — making it easy to analyze and track over time.
🧠What This Script Does
- Reads a list of servers from a text file (
servers.txt
) - Pings each server once
- Logs whether each server is UP (reachable) or DOWN (unreachable)
- Appends the latest results at the top of the CSV log file
- Adds a header row for clarity
How to Use
- Create a file named servers.txt in the same folder, listing one server per line:
- Save the above script as PingCheck.bat (Code is available below)
- Run it in Command Prompt
- Open PingResults.csv in Excel or Notepad to view the results
Why It’s Useful
- No third-party tools required — just pure Windows Batch scripting
- Keeps a timestamped log of server health
- Perfect for quick checks in small or mid-sized environments
Code:
====================PingServers_CSV.bat===================================
@echo offsetlocal enabledelayedexpansion
:: === Configuration ===
set "ServerList=servers.txt"
set "CsvFile=PingResults.csv"
set "TempFile=%TEMP%\PingTemp_%RANDOM%.csv"
:: === Create CSV header if file doesn’t exist ===
if not exist "%CsvFile%" (
echo Server,Status,Date,Time > "%CsvFile%"
)
:: === Verify server list exists ===
if not exist "%ServerList%" (
echo ERROR: %ServerList% not found!
echo Please create the file with one server per line.
pause
exit /b
)
:: === Loop through each server ===
for /f "usebackq tokens=* delims=" %%S in ("%ServerList%") do (
echo Pinging %%S...
ping -n 2 -w 1000 %%S >nul 2>&1
if !errorlevel! equ 0 (
set "status=SUCCESS"
) else (
set "status=FAIL"
)
:: === Format timestamp ===
for /f "tokens=1-4 delims=/ " %%a in ("%date%") do set curDate=%%d-%%b-%%c
for /f "tokens=1-2 delims=:." %%a in ("%time%") do set curTime=%%a:%%b
:: === Build new CSV line ===
set "newline=%%S,!status!,%date%,%time%"
:: === Insert new line right after header ===
>"%TempFile%" (
(for /f "tokens=* delims=" %%A in ('type "%CsvFile%" ^| findstr /n "^"') do (
for /f "tokens=1,* delims=:" %%B in ("%%A") do (
if %%B==1 (
echo %%C
echo !newline!
) else (
echo %%C
)
)
))
)
move /y "%TempFile%" "%CsvFile%" >nul
)
echo.
echo ✅ Done! Check "%CsvFile%" for results.
pause
for /f "tokens=1-4 delims=/ " %%a in ("%date%") do set curDate=%%d-%%b-%%c
for /f "tokens=1-2 delims=:." %%a in ("%time%") do set curTime=%%a:%%b
:: === Build new CSV line ===
set "newline=%%S,!status!,%date%,%time%"
:: === Insert new line right after header ===
>"%TempFile%" (
(for /f "tokens=* delims=" %%A in ('type "%CsvFile%" ^| findstr /n "^"') do (
for /f "tokens=1,* delims=:" %%B in ("%%A") do (
if %%B==1 (
echo %%C
echo !newline!
) else (
echo %%C
)
)
))
)
move /y "%TempFile%" "%CsvFile%" >nul
)
echo.
echo ✅ Done! Check "%CsvFile%" for results.
pause
=======================================================
No comments:
Post a Comment