How to ping multiple servers and return IP address and Hostnames using batch script?

后端 未结 8 1726
余生分开走
余生分开走 2020-12-28 10:45

So I have to use batch only for this. Basically, the server HOSTNAMES are all listed in a txt file. I used the following code to ping all the servers and di

8条回答
  •  难免孤独
    2020-12-28 11:37

    Parsing pingtest.txt for each HOST name and result with batch is difficult because the name and result are on different lines.

    It is much easier to test the result (the returned error code) of each PING command directly instead of redirecting to a file. It is also more efficient to enclose the entire construct in parens and redirect the final output just once.

    >result.txt (
      for /f %%i in (testservers.txt) do ping -n 1 %%i >nul && echo %%i UP||echo %%i DOWN
    )
    

提交回复
热议问题