Windows 如果无法到达目的地,如何将ping统计信息输入日志文件并将屏幕颜色更改为红色?

Windows 如果无法到达目的地,如何将ping统计信息输入日志文件并将屏幕颜色更改为红色?,windows,shell,batch-file,ping,script,Windows,Shell,Batch File,Ping,Script,我在这里找到了一个脚本并对其进行了编辑,但无法完成任务。我想要的是ping一个主机,该主机将由用户提供,并在屏幕上显示结果,以及将其写入日志文件。当无法访问主机时,窗口的颜色应更改为红色,并且ping统计信息也应附加到末尾的日志文件中 @echo off set /p host_title=Host Title: title %host_title% set /p host=Host Address: set logfile="Log_%host%.log" echo T

我在这里找到了一个脚本并对其进行了编辑,但无法完成任务。我想要的是ping一个主机,该主机将由用户提供,并在屏幕上显示结果,以及将其写入日志文件。当无法访问主机时,窗口的颜色应更改为红色,并且ping统计信息也应附加到末尾的日志文件中

@echo off
set /p host_title=Host Title:
title %host_title%

set /p host=Host Address:
set logfile="Log_%host%.log"

echo Target Host = %host% >%logfile%

for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (ping %host% -n 1 > nul
    IF ERRORLEVEL 1 (SET OUT=4f  & echo Request timed out.) ELSE (SET OUT=02)
    color %OUT%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL
    GOTO Ping)
当无法访问主机且程序退出后ping统计信息也未写入日志文件时,上述脚本在更改颜色时出现问题

ping统计数据应如下所示:

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 59ms, Maximum = 62ms, Average = 60ms