windows - Batch script for checking if a server is online -
i want have windows batch script goes through list of servers , checks every server ping if online. list of servers should simple plain text file , should this:
... "google" www.google.com "node1" 221.12.123.1 "download server" dl.myserver.com "login server" login.myserver.com ...
here simple rundown program should do:
- print list of descriptions of servers in list screen.
- ping first server server 4 times if 1 ping succeeds should return online if 4 pings fail should return offline.
- print online or offline next first server in printed list
- run step 2 , 3 other servers in list.
the output should following:
... google: online stackoverflow: online node1: online download server: offline login server: offline ...
i want know if possible in (windows) batch , how it. if isn't possible in batch, programming language should use? possible program in python?
i thankful if post code how this, thanks!
@echo off setlocal enableextensions enabledelayedexpansion /f usebackq^ tokens^=1^,2^ delims^=^" %%a in ("servers.txt") ( call :isonline %%b && set "status=online" || set "status=offline" echo %%a : !status! ) endlocal exit /b :isonline address setlocal enableextensions disabledelayedexpansion :: temporary file needed capture ping output later processing set "tempfile=%temp%\%~nx0.%random%.tmp" :: ping indicated address , errorlevel ping -w 1000 -n 4 %~1 > "%tempfile%" && set "pingerror=" || set "pingerror=1" :: when pinging, :: :: errorlevel = 1 when :: ipv4 - when packet lost. necessary check "ttl=" :: string in output of ping command. :: ipv6 - when packet lost. :: errorlevel = 0 when :: ipv4 - packets received. pinging inactive host on :: same subnet result in no packet lost. necessary :: check "ttl=" string in output of ping command. :: ipv6 - @ least 1 packet reaches host. :: :: +--------------+-------------+ :: | ttl= present | no ttl | :: +-----------------------+--------------+-------------+ :: | ipv4 errorlevel 0 | ok | error | :: | errorlevel 1 | ok | error | :: +-----------------------+--------------+-------------+ :: | ipv6 errorlevel 0 | | ok | :: | errorlevel 1 | | error | :: +-----------------------+----------------------------+ :: :: so, if ttl= present in output, host online. if errorlevel 0 :: , address ipv6 host online. in rest of cases :: host offline. :: :: determine ip version, regular expresion match ipv6 :: address used findstr. tested in case :: of no errorlevel, ip address should present in output of :: ping command. set "exitcode=1" find "ttl=" "%tempfile%" >nul 2>nul && set "exitcode=0" || ( if not defined pingerror ( findstr /r /c:" [a-f0-9:][a-f0-9]*:[a-f0-9:%%]*[a-f0-9]: " "%tempfile%" >nul 2>nul && set "exitcode=0" ) ) :: cleanup , return errorlevel if exist "%tempfile%" del /q "%tempfile%" >nul 2>nul endlocal & exit /b %exitcode%
Comments
Post a Comment