Batch file to run ping command and output to text

2020-07-27 05:11发布

I have two batch files to ping all IPs in a text file servers.txt. One of the batch files pings the servers and shows the result on the CMD window. 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.

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 .

Here is first batch file which shows pings on CMD windows without any output to text file :

  @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 :

 @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.

3条回答
Emotional °昔
2楼-- · 2020-07-27 05:43

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 :

enter image description here

查看更多
beautiful°
3楼-- · 2020-07-27 06:03

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
    )
)
查看更多
混吃等死
4楼-- · 2020-07-27 06:05

You could simply implement explicit redirection to the con device (console).
By the way, you actually do not need delayed expansion if you use the if ErrorLevel syntax:

@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
        )
    )
)
查看更多
登录 后发表回答