txt 파일에 나열된 문자열로 IP 목록을 핑 (Ping)하여 배치를 사용해야합니다. SERVER_ADDRESS=ADDRESS N/A for

옆에 텍스트 문자열을 입력하여 IP 목록을 핑하고 싶습니다. 텍스트는 여러 단어이며 숫자가 있습니다. IP는 모두 10.xxx로 시작합니다

이것은 내가 조사 한 스크립트이지만 내가 넣은 IP의 IP를 확인하려고합니다. 호스트 이름을 거기에 넣으면 효과가 있다고 생각합니다. 어쩌면 경우에 대비하여 거기에 보관해야 할 수도 있습니다.

@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (testservers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS=%%y
        if %%x==Reply set SERVER_ADDRESS=%%z
        if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! >>%OUTPUT_FILE%
)

내가 정말로 원하는 것은 result.txt 파일 의 줄 끝에서 “This is the Server XYZ”와 같은 텍스트 문자열을 갖는 것 입니다.

따라서 내 testservers.txt 파일은 다음과 같습니다.

10.0.0.1 This is the Server ABC.
10.0.0.2 This is the Server DEF.
hostname1 This is the Server LMN.
hostname2 This is the Server XYZ.

지금 실행하면 이와 같은 결과가 result.txt 파일에 출력됩니다.

10.0.0.1 [10.0.0.1] is UP
10.0.0.1 [10.0.0.2] is UP
hostname1 [10.0.0.3] is UP
hostname2 [10.0.0.4] is UP

그러면 result.txt 파일은 다음과 같습니다.

10.0.0.1 [10.0.0.1] is UP This is the Server ABC.
10.0.0.2 [10.0.0.2] This is the Server DEF.
hostname1 [10.0.0.3] This is the Server LMN.
hostname2 [10.0.0.4] This is the Server XYZ.

충분한 정보를 제공했으면합니다. 내가하지 않았다면 알려주십시오.



답변

FOR /F루프를 사용하여 testservers.txt 의 텍스트 문서 내용을 읽은 후 ” TOKENS=1,*” 를 추가 하면 첫 번째 토큰이 각 줄당 서버 이름 또는 IP 주소가되고 다음 토큰이 나머지 부분이됩니다. 그 후 각 줄.

이것은 당신이 다음의 다음의 토큰 활용할 수 있다는 것을 의미합니다 FOR /F첫 번째 토큰 후 각 라인의 나머지 부분을 얻기 위해 루프를하고 해당를 추가 ECHO위한 라인 %OUTPUT_FILE%.

testservers.txt

여기에 이미지 설명을 입력하십시오


스크립트 예

@echo off
setlocal enabledelayedexpansion

set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f "tokens=1,*" %%i in (testservers.txt) do (
    set SERVER_ADDRESS=ADDRESS N/A
    for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS=%%y
        if %%x==Reply set SERVER_ADDRESS=%%z
        if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
    )
    echo %%i [!SERVER_ADDRESS::=!] is !SERVER_STATE! %%j >>%OUTPUT_FILE%
)

결과

10.0.0.1 [10.0.0.1] is DOWN This is the Server ABC.
10.0.0.2 [10.0.0.2] is DOWN This is the Server DEF.
hostname1 [<IP Address>] is UP This is the Server LMN.
hostname2 [<IP Address>] is UP This is the Server XYZ.

추가 자료

  • / F

  • FOR /?

        tokens=x,y,m-n  - specifies which tokens from each line are to
                          be passed to the for body for each iteration.
                          This will cause additional variable names to
                          be allocated.  The m-n form is a range,
                          specifying the mth through the nth tokens.  If
                          the last character in the tokens= string is an
                          asterisk, then an additional variable is
                          allocated and receives the remaining text on
                          the line after the last token parsed.
    

답변