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

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

问题描述

我基本上想有一个Windows批处理脚本,经过服务器,并检查了平每台服务器的列表,如果是在网上。
服务器列表应该是一个简单的纯文本文件,应该是这个样子:

  ...
谷歌www.google.com
节点1221.12.123.1
下载服务器dl.myserver.com
登录服务器login.myserver.com
...

下面是一个简单的破败程序应该做的:


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

  2. 平的第一台服务器服务器,如果4次一平成功,它应该重新联机如果所有4 ping失败它应该返回脱机状态。

  3. 打印联机或脱机旁边的第一个服务器中的打印列表

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

输出应如下所示:

  ...
谷歌:在线
计算器:在线
节点1:在线
下载服务器:下线
登录服务器:下线
...

我只是想知道,如果这甚至有可能在(视窗)批次和如何做到这一点。如果它是不可能在批处理,我应该使用什么编程语言?有没有可能在Python编程呢?

我也将是充满感激,如果任何人都可以张贴code如何做到这一点,谢谢!


解决方案

 关闭@echo    SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion    FOR / F有usebackq ^ ^代币= 1 ^ 2 ^ ^ delims = ^%%一中(servers.txt),做(
        拨打:isOnline %% B和;&安培;设置状态=在线||设置状态=离线
        回声%%一个:状态!
    )    ENDLOCAL    退出/ B:isOnline地址
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion    ::需要一个临时文件,以捕获以后处理平输出
    设置临时文件=%TEMP%\\%〜NX0%随机%的.tmp    :: ping到指定地址,并得到错误级别
    平-w 1000 -n 4%〜1> %临时文件%&放大器;&安培;设置pingError =||设置pingError = 1    :: ping命令后,
    ::
    ::我们获得错误级别= 1时,
    :: IPv4的 - 当任何数据包丢失。有必要检查TTL =
    :: string的ping命令的输出。
    :: IPv6的 - 当所有的数据包都将丢失。
    ::我们获得错误级别= 0时,
    :: IPv4的 - 收到的所有数据包。但ping命令对一个非活动主机
    ::同一子网结果没有数据包丢失。有必要
    ::为您在ping命令的输出TTL =字符串。
    :: IPv6的 - 至少一个分组到达主机。
    ::
    :: + -------------- + ------------- +
    :: | TTL = present |没有TTL |
    :: + ----------------------- + -------------- + -------- ----- +
    :: | IPv4的错误级别0 | OK | ERROR |
    :: | ERRORLEVEL 1 | OK | ERROR |
    :: + ----------------------- + -------------- + -------- ----- +
    :: | IPv6的错误级别0 | | OK |
    :: | ERRORLEVEL 1 | | ERROR |
    :: + ----------------------- + ----------- ------------ ----- +
    ::
    ::所以,如果TTL =是输出present,主机联机。如果错误级别为0
    ::和地址是IPv6的那么主机联机。在其余情形下
    ::主机处于脱机状态。
    ::
    ::要确定IP版本,一个普通的前presion公司匹配的IPv6
    ::地址用于与FINDSTR。如将在的情况下只测试
    ::没有错误级别的,IP地址应在输出present
    :: ping命令。    一套退出code = 1
    找到TTL =%临时文件%> NUL 2 - ; NUL&放大器;&安培;一套退出code = 0|| (
        如果没有定义pingError(
            FINDSTR / R / C:[A-f0-9:] [A-f0-9] * [A-f0-9:%%] * [A-f0-9]:%临时文件%&GT ; NUL 2 - ; NUL&放大器;&安培;一套退出code = 0
        )
    )    ::清理和返回错误级别
    如果存在%临时文件%DEL / Q%临时文件%> NUL 2 - ; NUL
    ENDLOCAL&安培;退出/ B%出口code%

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. print a list of the descriptions of all the servers in the list to the screen.
  2. ping the first server server 4 times if one ping succeeds it should return online if all 4 pings fail it should return offline.
  3. print online or offline next to the first server in the printed list
  4. run step 2 and 3 for all other servers in the list.

The output should look like the following:

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

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天全站免登陆