2014-05-17 3 views
0

기본적으로 서버 목록을 통과하여 모든 서버에 ping이 있는지 온라인으로 확인하는 Windows 배치 스크립트가 필요합니다. 서버 목록은 간단한 일반 텍스트 파일이어야하며 모양은 다음과 같습니다서버가 온라인 상태인지 확인하기위한 일괄 처리 스크립트

  1. 인쇄의 설명 목록 : 여기
    ... 
    "Google" www.google.com 
    "Node1" 221.12.123.1 
    "Download Server" dl.myserver.com 
    "Login Server" login.myserver.com 
    ... 
    

    프로그램이 무엇을해야 간단한 개요입니다 목록에있는 모든 서버를 화면에 표시합니다.
  2. 한 번의 핑이 성공하면 첫 번 째 서버 서버를 4 번 핑합니다. 4 번 모두 실패하면 오프라인이됩니다.
  3. 인쇄 된 목록의 첫 번째 서버 옆에 온라인 또는 오프라인으로 인쇄하십시오.
  4. 목록의 다른 모든 서버에 대해 2 단계와 3 단계를 실행하십시오.

출력은 다음과 같이한다 :이 (창) 배치 방법을 수행하는 방법에서도 가능하면

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

난 그냥 알고 싶어요. 일괄 처리가 불가능한 경우 어떤 프로그래밍 언어를 사용해야합니까? 이것을 파이썬으로 프로그래밍 할 수 있습니까?

아무도 코드를 게시 할 수 있다면 정말 감사 할 것입니다. 감사합니다!

답변

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

이 일괄 적으로 쉽게 수행 할 수 있습니다, 당신은 그냥 for /f 루프, echo 문, if 문, goto/call 문을 필요로하고 ping 명령을 사용합니다.

1.print 화면 목록에있는 모든 서버의 설명 목록.

당신은 하나의 ping이 4 핑은 오프라인 반환해야 실패하는 경우가 온라인 반환해야 성공하면 첫 번째 서버 서버 4 번 2.ping echo "Google" www.google.com

처럼,이에 대한 echo 문을 사용할 수 있습니다. [for /f "tokens=5 delims==, " %%p in (처럼]을 for /f 루프 내부

, 당신은 인쇄 목록의 첫 번째 서버 옆에 온라인 또는 오프라인으로 너무 ping -n 4 www.google.com

3.print을 추천 할려고로 ping 명령을 사용할 수 있습니다

당신은 사용할 수 있습니다 여기에 if statemenet, 그래서 같은 : if "%status%"=="online" echo Google: online하거나 echo Google: %status%

,

4. 목록의 다른 모든 서버에 대해 2 단계와 3 단계를 실행하십시오.

당신은 여기 goto 또는 call 문을 사용하여 예를 들어, (함수처럼 사용) 할 수 있습니다 call :server_status_function www.google.com

+0

당신이 여기 스크립트의 전체 코드를 게시하시기 바랍니다 수 있을까? 파일에서 설명과 IP 주소를 얻는 방법을 이해할 수 없기 때문입니다. –

관련 문제