Ping ip 地址并将结果保存到 txt 文件。

Ping ip 地址并将结果保存到 txt 文件。

我正在尝试打开 IP 地址列表并 ping 它们,然后将响应保存到 TXT 文件。ping 循环运行,没有问题,并且报告正确,但我可以让它将结果保存到文本文件。

@echo off

SET LOGFILE=MyLogFile.log
call :Logit >> %LOGFILE% 
exit /b 0

for /f "delims=" %%a in ( ' type "C:\Users\kelly\Desktop\Ping\computerlist.txt" ' ) do ping -n 1 %%a >nul && (echo %%a ok >> %LOGFILE% ) || (echo %%a failed to respond >> %LOGFILE% ) 
pause

答案1

@echo off
SET LOGFILE=C:\Temp\MyLogFile.log
SET PINGLIST=C:\Users\kelly\Desktop\Ping\computerlist.txt
for /f "delims=" %%a in (%PINGLIST%) DO ping -n 1 %%a > nul && (echo %%a is ok >> %LOGFILE%) || (echo %%a is unreachable! >> %LOGFILE%)

只需确保您的计算机列表每行只有一个主机名。

输入(computerlist.txt)

gooobler
google.com
msn.com
localhost

输出(MyLogFile.log)

gooobler is unreachable! 
google.com is ok 
msn.com is unreachable! 
localhost is ok 

答案2

你有 do ping -n 1 %%a >nul

这会获取 ping 输出并将其丢弃。

我想你想要

do ping -n 1 %%a >%LOGFILE%

另外,您正在调用一个不存在的区域/子程序(Logit)

@echo off

SET LOGFILE=MyLogFile.log
call :Logit >> %LOGFILE% 
exit /b 0

:Logit
for /f "delims=" %%a in ( ' type "C:\Users\kelly\Desktop\Ping\computerlist.txt" ' ) do ping -n 1 %%a >nul && (echo %%a ok >> %LOGFILE% ) || (echo %%a failed to respond >> %LOGFILE% ) 
pause

(未经测试)

~~根据 OP 的评论进行编辑并且已经测试过~~

我想这就是你要找的东西:

@ECHO OFF
SET LOGFILE=logFile.txt
SET TEMP_FILE=temp.txt
SET PINGLIST=comps.txt


REM contatenate log file
echo. > %LOGFILE%

for /f "delims=" %%a in (%PINGLIST%) do (
    REM do ping and store response - using a temp file because we care about the response more than once.
    ping -n 1 %%a > %TEMP_FILE%

    REM append response to log file
    type %TEMP_FILE% >> %LOGFILE%

    REM from: https://stackoverflow.com/a/8531199
    REM check if ping determined the host is unreachable by searching the output
    findstr /c:"Destination host unreachable" /c:"could not find host" %TEMP_FILE% > NUL

    REM if the errorlevel is at least 1 (unsuccessful at finding string)
    if errorlevel 1 (
        REM then must have reached host
        echo %%a is ok >> %LOGFILE%
    ) ELSE (
        REM else, we found the string that sais we cannot reach the host
        echo %%a is UNREACHABLE^! >> %LOGFILE%
    )
    REM cleanup
    del %TEMP_FILE%
)
exit /b

答案3

我的要求略有不同,所以我最终得到的结果是,我想要一个可以无限循环的程序。

我想要一个文件仅有的显示无响应的 ping 及其发生时间

我希望看到一些迹象表明 CMD 窗口没有停滞

所以我添加了时间,errorlist.txt并做了一些调整,试图让它工作

我将在两台电脑上安装它,这样如果其中一台电脑出现网络错误,另一台电脑就可以snitch修复。

通过从每台 PC 和几个互联网 IP 地址 ping 网络上的每台设备,它应该能够很多更容易找到间歇性问题,并确定是本地问题还是硬件问题导致的

@ECHO OFF

CD C:\PingTest


SET LOGFILE=logFile.txt

SET TEMP_FILE=temp.txt

SET PINGLIST=comps.txt

SET ERRORLIST=ErrorList.txt

echo. > %LOGFILE%
echo. > %ERRORLIST%

:START

cls

echo Pingtest Running

Time /T

REM Change the number after /t to pause for X seconds between loops. IE, /t 10 > NUL pauses for 10 seconds before running again

timeout /t 5 > NUL


Time /T >> %LOGFILE%
@ECHO OFF

echo. " " >> %LOGFILE%

REM contatenate log file
echo. > %LOGFILE%

REM contatenate log file



for /f "delims=" %%a in (%PINGLIST%) do (
    REM do ping and store response - using a temp file because we care about the response more than once.
    ping -n 1 %%a > %TEMP_FILE%

    REM append response to log file
    type %TEMP_FILE% >> %LOGFILE%

    REM from: https://stackoverflow.com/a/8531199
    REM check if ping determined the host is unreachable by searching the output
    findstr /c:"Destination host unreachable" /c:"could not find host" %TEMP_FILE% > NUL

    REM if the errorlevel is at least 1 (unsuccessful at finding string)
    if errorlevel 1 (
        REM then must have reached host
        echo %%a is ok >> %LOGFILE%
    ) ELSE (
        REM else, we found the string that sais we cannot reach the host
    Time /T >> %ERRORLIST%
        echo %%a is UNREACHABLE^! >> %ERRORLIST%
    )
    REM cleanup
    del %TEMP_FILE%
)

Goto :START

相关内容