【问题标题】:Batch script for checking if a server is online用于检查服务器是否在线的批处理脚本
【发布时间】:2014-07-06 00:08:43
【问题描述】:

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

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

下面是程序应该做什么的简单概述:

  1. 将列表中所有服务器的描述列表打印到屏幕上。
  2. ping 第一个服务器服务器 4 次,如果一个 ping 成功,它应该返回在线,如果所有 4 个 ping 都失败,它应该返回离线。
  3. 在打印列表中的第一台服务器旁边在线或离线打印
  4. 为列表中的所有其他服务器运行第 2 步和第 3 步。

输出应如下所示:

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

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

如果有人能发布如何执行此操作的代码,我也将非常感激,谢谢!

【问题讨论】:

    标签: windows batch-file networking network-programming ping


    【解决方案1】:
    @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 10 1803 中,您可以使用 CURL 而不是 ping。成功和失败检测更容易:curl -s -m 2 servername > nul && echo offline || echo online
    【解决方案2】:

    这可以很容易地批量完成,您只需要一些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 次,如ping -n 4 www.google.com

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

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

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

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

    【讨论】:

    • 你能把脚本的全部代码贴在这里吗?因为我不明白如何从文件中获取描述和 IP 地址。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多