用于检查服务器是否在线的批处理脚本 [英] Batch script for checking if a server is online

查看:24
本文介绍了用于检查服务器是否在线的批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想要一个 Windows 批处理脚本,它通过服务器列表并检查每个服务器是否在线.服务器列表应该是一个简单的纯文本文件,应该如下所示:

I basically want to have a windows batch script which goes through a list of servers and checks every server with a ping if it is online. The list of servers should be a simple plain text file and should look something like this:

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

这里是程序应该做的一个简单的纲要:

Here is a simple rundown what the program should do:

  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 编程吗?

I just want to know if this is even possible in (windows) batch and how to do it. If it isn't possible in batch, what programming language should I use? Would it be possible to program this in Python?

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

I would also be really thankful if anybody could post the code how to do this, Thanks!

推荐答案

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

这篇关于用于检查服务器是否在线的批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆