windows – 用于检查服务器是否在线的批处理脚本
发布时间:2020-12-13 20:37:29 所属栏目:Windows 来源:网络整理
导读:我基本上想要一个Windows批处理脚本,它通过一个服务器列表,并检查每个服务器是否在线ping. 服务器列表应该是一个简单的纯文本文件,应该如下所示: ..."Google" www.google.com"Node1" 221.12.123.1"Download Server" dl.myserver.com"Login Server" login.my
我基本上想要一个Windows批处理脚本,它通过一个服务器列表,并检查每个服务器是否在线ping.
服务器列表应该是一个简单的纯文本文件,应该如下所示: ... "Google" www.google.com "Node1" 221.12.123.1 "Download Server" dl.myserver.com "Login Server" login.myserver.com ... 这是一个简单的纲要,程序应该做什么: >将列表中所有服务器的描述列表打印到屏幕上. 输出应如下所示: ... Google: online Stackoverflow: online Node1: online Download Server: offline Login server: offline ... 我只是想知道这是否可能在(windows)批处理中以及如何进行.如果批量不可能,我应该使用什么编程语言?是否有可能在Python中编程? 如果有人能发布代码怎么做,我也会非常感谢,谢谢! @echo off setlocal enableextensions enabledelayedexpansion for /f usebackq^ tokens^=1^,2^ delims^=^" %%a in ("servers.txt") do ( call :isOnline %%b && set "status=online" || set "status=offline" echo %%a : !status! ) endlocal exit /b :isOnline address setlocal enableextensions disabledelayedexpansion :: a temporary file is needed to capture ping output for later processing set "tempFile=%temp%%~nx0.%random%.tmp" :: ping the indicated address and get errorlevel ping -w 1000 -n 4 %~1 > "%tempFile%" && set "pingError=" || set "pingError=1" :: When pinging,:: :: we get errorlevel = 1 when :: ipv4 - when any packet is lost. It is necessary to check for "TTL=" :: string in the output of the ping command. :: ipv6 - when all packet are lost. :: we get errorlevel = 0 when :: ipv4 - all packets received. But pinging a inactive host on the :: same subnet result in no packet lost. It is necessary to :: check for "TTL=" string in the output of the ping command. :: ipv6 - at least one packet reaches the host. :: :: +--------------+-------------+ :: | TTL= present | No TTL | :: +-----------------------+--------------+-------------+ :: | ipv4 errorlevel 0 | OK | ERROR | :: | errorlevel 1 | OK | ERROR | :: +-----------------------+--------------+-------------+ :: | ipv6 errorlevel 0 | | OK | :: | errorlevel 1 | | ERROR | :: +-----------------------+----------------------------+ :: :: So,if TTL= is present in output,host is online. If errorlevel is 0 :: and the address is ipv6 then host is online. In the rest of the cases :: the host is offline. :: :: To determine the ip version,a regular expresion to match a ipv6 :: address is used with findstr. As it will be only tested in the case :: of no errorlevel,the ip address should be present in the 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 and return errorlevel if exist "%tempFile%" del /q "%tempFile%" >nul 2>nul endlocal & exit /b %exitCode% (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在允许的进程之间传递一个Windows安全令牌
- .net – Metro应用程序 – ListView – 如何替换ListViewIt
- 链接许多.libs来制作DLL:未解析的外部符号_DllMainCRTStar
- 单元测试 – 如何在.NET中伪造Azure表存储单元测试?
- windows-xp – 将Windows休眠文件移动到其他驱动器
- DevExpress20:XtraCharts控件实现图表
- windows – 如何防止MsDepSvc.exe启动?
- 如何将公共证书导入Windows Azure?
- windows-server-2012-r2 – 扩展卷失败,但分区已扩展
- .net – 如何与我自己的Windows服务进行安全通信
推荐文章
站长推荐
热点阅读