windows – 用于检查服务器是否在线的批处理脚本

前端之家收集整理的这篇文章主要介绍了windows – 用于检查服务器是否在线的批处理脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我基本上想要一个Windows批处理脚本,它通过一个服务器列表,并检查每个服务器是否在线ping.
服务器列表应该是一个简单的纯文本文件,应该如下所示:
  1. ...
  2. "Google" www.google.com
  3. "Node1" 221.12.123.1
  4. "Download Server" dl.myserver.com
  5. "Login Server" login.myserver.com
  6. ...

这是一个简单的纲要,程序应该做什么:

>将列表中所有服务器的描述列表打印到屏幕上.
>如果一次ping成功,则ping第一台服务器服务器4次,如果所有4次ping失败,它应该返回联机状态,它应该返回脱机状态.
>在打印列表中第一台服务器旁边的在线或离线打印
>对列表中的所有其他服务器运行步骤2和3.

输出应如下所示:

  1. ...
  2. Google: online
  3. Stackoverflow: online
  4. Node1: online
  5. Download Server: offline
  6. Login server: offline
  7. ...

我只是想知道这是否可能在(windows)批处理中以及如何进行.如果批量不可能,我应该使用什么编程语言?是否有可能在Python中编程?

如果有人能发布代码怎么做,我也会非常感谢,谢谢!

  1. @echo off
  2.  
  3. setlocal enableextensions enabledelayedexpansion
  4.  
  5. for /f usebackq^ tokens^=1^,2^ delims^=^" %%a in ("servers.txt") do (
  6. call :isOnline %%b && set "status=online" || set "status=offline"
  7. echo %%a : !status!
  8. )
  9.  
  10. endlocal
  11.  
  12. exit /b
  13.  
  14. :isOnline address
  15. setlocal enableextensions disabledelayedexpansion
  16.  
  17. :: a temporary file is needed to capture ping output for later processing
  18. set "tempFile=%temp%\%~nx0.%random%.tmp"
  19.  
  20. :: ping the indicated address and get errorlevel
  21. ping -w 1000 -n 4 %~1 > "%tempFile%" && set "pingError=" || set "pingError=1"
  22.  
  23. :: When pinging,::
  24. :: we get errorlevel = 1 when
  25. :: ipv4 - when any packet is lost. It is necessary to check for "TTL="
  26. :: string in the output of the ping command.
  27. :: ipv6 - when all packet are lost.
  28. :: we get errorlevel = 0 when
  29. :: ipv4 - all packets received. But pinging a inactive host on the
  30. :: same subnet result in no packet lost. It is necessary to
  31. :: check for "TTL=" string in the output of the ping command.
  32. :: ipv6 - at least one packet reaches the host.
  33. ::
  34. :: +--------------+-------------+
  35. :: | TTL= present | No TTL |
  36. :: +-----------------------+--------------+-------------+
  37. :: | ipv4 errorlevel 0 | OK | ERROR |
  38. :: | errorlevel 1 | OK | ERROR |
  39. :: +-----------------------+--------------+-------------+
  40. :: | ipv6 errorlevel 0 | | OK |
  41. :: | errorlevel 1 | | ERROR |
  42. :: +-----------------------+----------------------------+
  43. ::
  44. :: So,if TTL= is present in output,host is online. If errorlevel is 0
  45. :: and the address is ipv6 then host is online. In the rest of the cases
  46. :: the host is offline.
  47. ::
  48. :: To determine the ip version,a regular expresion to match a ipv6
  49. :: address is used with findstr. As it will be only tested in the case
  50. :: of no errorlevel,the ip address should be present in the output of
  51. :: ping command.
  52.  
  53. set "exitCode=1"
  54. find "TTL=" "%tempFile%" >nul 2>nul && set "exitCode=0" || (
  55. if not defined pingError (
  56. findstr /r /c:" [a-f0-9:][a-f0-9]*:[a-f0-9:%%]*[a-f0-9]: " "%tempFile%" >nul 2>nul && set "exitCode=0"
  57. )
  58. )
  59.  
  60. :: cleanup and return errorlevel
  61. if exist "%tempFile%" del /q "%tempFile%" >nul 2>nul
  62. endlocal & exit /b %exitCode%

猜你在找的Windows相关文章