2016-12-24 83 views
1

我有兩個批處理文件ping文本文件中的所有IP servers.txt。 其中一個批處理文件ping服務器並在CMD窗口中顯示結果。 另一個批處理文件ping服務器,並且在CMD窗口上什麼也沒有顯示,並且在它完成對服務器的所有ping之後,它將把它們放在OnlineServers.txt文件中。批處理文件運行ping命令並輸出到文本以及

我想混合這個東西。

我希望批處理文件運行ping並在屏幕上顯示它們,並將所有在線服務器放在OnlineServers.txt中。

這是第一個批處理文件,顯示在CMD窗口坪沒有任何輸出到文本文件:

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

echo. 
pause 

這裏是第二批文件,該文件顯示在CMD窗口,只將它輸出坪後的文件什麼所有服務器:

@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 

這些超過150臺服務器來檢查,我每天都將服務器添加到這個列表所以它的一個長長的清單來檢查。

回答

0

寫入該文件的批處理將必須使用重定向中斷附帶括號的圓括號 並在開始時重置文件。

@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 
    ) 
) 
1

您可以簡單地實現顯式重定向到con設備(控制檯)。
順便說一句,你其實並不如果使用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 
     ) 
    ) 
) 
0

如果你想添加一些顏色供您批處理文件這樣的:

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

所以你可以得到像這樣的輸出:

enter image description here

相關問題