简体   繁体   English

运行 ping 命令并输出到文本的批处理文件

[英]Batch file to run ping command and output to text aswell

I have two batch files to ping all IPs in a text file servers.txt .我有两个批处理文件来 ping 文本文件servers.txt所有 IP。 One of the batch files pings the servers and shows the result on the CMD window.其中一个批处理文件 ping 服务器并在 CMD 窗口中显示结果。 The other batch file pings the servers and shows nothing on CMD windows and after it finished all the pings to servers , it will put them in the OnlineServers.txt file.另一个批处理文件 ping 服务器并在 CMD 窗口上不显示任何内容,在完成对服务器的所有 ping 后,它将把它们放在OnlineServers.txt文件中。

I want to mix this thing.我想混合这个东西。

I want the batch file to run pings and show them on screen and put all the online servers in the OnlineServers.txt .我希望批处理文件运行 ping 并将它们显示在屏幕上,并将所有在线服务器放在OnlineServers.txt

Here is first batch file which shows pings on CMD windows without any output to text file :这是第一个批处理文件,它在 CMD 窗口上显示 ping,没有任何输出到文本文件:

  @echo off
  for /f "delims=" %%a in (servers.txt) do ping -n 1 %%a >nul && (echo %%a      Online) || (echo %%a        Offline)

 echo.
 pause

And here is the second batch file which shows nothing on CMD window and only output the file after it pings all the servers :这是第二个批处理文件,它在 CMD 窗口上不显示任何内容,仅在 ping 所有服务器后才输出文件:

 @echo off
 setlocal EnableDelayedExpansion
 (for /F "delims=" %%a in (servers.txt) do (
 ping -n 1 "%%a" > NUL
 if !errorlevel! equ 0 (
  echo %%a      Online
 )
 )) > OnlineServers.txt

These is more than 150 servers to check and I add servers to this list every day so its a long list to check.要检查的服务器超过 150 台,我每天都会将服务器添加到此列表中,因此要检查的列表很长。

You could simply implement explicit redirection to the con device (console).您可以简单地实现对con设备(控制台)的显式重定向。
By the way, you actually do not need delayed expansion if you use the if ErrorLevel syntax:顺便说一句,如果您使用if ErrorLevel语法,您实际上不需要延迟扩展:

@echo off
setlocal
> "OnlineServers.txt" (
    for /F "usebackq delims=" %%a in ("servers.txt") do (
        ping -n 1 "%%a" > NUL
        if not ErrorLevel 1 (
            echo %%a      Online> con
            echo %%a      Online
        ) else (
            echo %%a      Offline> con
        )
    )
)

The batch writing to the file will have to breakoff the enclosing parentheses with the redirection and reset the file at start.对文件的批量写入必须通过重定向断开括起来的括号并在开始时重置文件。

@echo off
setlocal EnableDelayedExpansion

:: Reset file
Type Nul >OnlineServers.txt

for /F "delims=" %%a in (servers.txt) do (
    ping -n 1 "%%a" >NUL 2>&1
    If !errorlevel! equ 0 (
       echo %%a      Online
       echo %%a      Online>>OnlineServers.txt
    ) Else (
       echo %%a      Offline
    )
)

If you want to add some colors for your batch file like that :如果您想为批处理文件添加一些颜色,如下所示:

@echo off
Title Multi-Ping hosts Tester with colors by Hackoo 2016
call :init
set "Servers_List=servers.txt"
If Not exist %Servers_List% goto error
mode con cols=70 lines=35
set "LogFile=OnlineServers.txt"
If exist %LogFile% Del %LogFile%
echo(
call :color 0E "      ------- Ping status of targets hosts -------" 1
echo(
(
    echo ******************************************************
    echo   PingTest executed on %Date% @ Time %Time%
    echo ******************************************************
    echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%Servers_List%") do (
    ping -n 1 %%a | find "TTL=" >nul
    if errorlevel 1 (
        call :color 0C " Host %%a is not reachable KO" 1
    ) else (
        call :color 0A " Host %%a is reachable OK" 1 & echo Host %%a is reachable OK >>%LogFile%
    )
)
EndLocal
Start "" %LogFile%
pause>nul & exit
::************************************************************************************* 
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::*************************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::*************************************************************************************
:error
mode con cols=70 lines=3
color 0C
cls
echo(
 echo       ATTENTION ! ! !  Check if the file "%Servers_List%" exists !
pause>nul & exit
::*************************************************************************************

So you can get as output like this one :所以你可以得到这样的输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM