微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我基本上想要一个Windows批处理脚本,通过一个服务器列表,并检查每个服务器与一个ping,如果它在线。 服务器列表应该是一个简单的纯文本文件,应该看起来像这样:

... "Google" www.google.com "Node1" 221.12.123.1 "Download Server" dl.myserver.com "Login Server" login.myserver.com ...

这是一个简单的程序应该做的事情:

将列表中所有服务器的描述列表打印到屏幕上。

ping第一台服务器服务器4次,如果一次ping成功,它应该返回在线,如果所有4个ping失败它应该离线。

在打印列表中的第一个服务器旁边在线或离线打印

对列表中的所有其他服务器运行步骤2和3。

输出应该如下所示:

... Google: online Stackoverflow: online Node1: online Download Server: offline Login server: offline ...

我只想知道这是甚至可以在(Windows)批处理和如何做到这一点。 如果批量不可能,我应该使用哪种编程语言? 是否有可能在Python中编程?

你如何将sockaddr结构转换为sockaddr_in – C ++networking套接字ubuntu UDP

如何在几个程序中接收相同的udpstream?

SCTP是否像使用Linux公布的那样工作?

sendto的最大缓冲区长度?

Windows无限期地为一个不存在的进程保留一个监听套接

我也会非常感激,如果有人可以发布代码如何做到这一点,谢谢!

有多less线程创build和什么时候?

如何以编程方式启用/禁用networking连接选项

需要删除与iptablesbuild立的连接

XBox 360 TCP堆栈不响应具有0字节有效负载的TCP零窗口探测器

如何在内核模块中将networking接口设置为混杂模式?

@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%

这可以很容易地批量完成,只需要一些for /f循环, echo语句, if语句, goto / call语句和使用ping命令。

1.将列表中所有服务器的描述列表打印到屏幕上。

您可以使用echo声明,如echo "Google" www.google.com

2. ping第一台服务器服务器4次,如果一次ping成功,应该返回在线,如果所有4个ping失败,它应该离线。

一个for /f循环中[比如for /f "tokens=5 delims==," %%p in ( ]),你可以使用ping命令和4个trys像这样ping -n 4 www.google.com

3.打印列表中第一个服务器旁边的在线或离线打印

你可以使用和if statemenet在这里,如: if "%status%"=="online" echo Google: online或只是echo Google: %status%

4.对列表中的所有其他服务器运行第2步和第3步。

您可以在此处使用goto或call语句(使用它像一个函数),例如: call :server_status_function www.google.com

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐