You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改Ping超时错误提示:批处理脚本实现求助

如何替换Ping命令中“Request Timed out”为自定义提示?

你正在编写一个带窗口置顶功能的Ping检测批处理脚本,希望将Ping输出中的Request timed out替换为自定义提示Host didn't respond,但当前脚本无法实现该需求。原脚本如下:

@ECHO OFF
@powershell -ExecutionPolicy UnRestricted -Command "(Add-Type -memberDefinition \"[DllImport(\"\"user32.dll\"\")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,int y,int cx, int xy, uint flagsw);\" -name \"Win32SetWindowPos\" -passThru )::SetWindowPos((Add-Type -memberDefinition \"[DllImport(\"\"Kernel32.dll\"\")] public static extern IntPtr GetConsoleWindow();\" -name \"Win32GetConsoleWindow\" -passThru )::GetConsoleWindow(),-1,0,0,0,0,67)"
title Ping tool
mode con: cols=50 lines=25
:PING
cls & set /p address= [*] Host to ping:
@echo [*] Started pinging at: %time%
title Pinging %address%
if %ERRORLEVEL% == 1 echo Host didn't respond.
ping %address% -t & echo. & pause. & goto :PING

当前Ping输出会包含默认超时提示,而你期望将Request timed out替换为Host didn't respond。


解决方案:实时过滤Ping输出

原脚本的问题在于%ERRORLEVEL%的判断时机完全不对——ping -t是持续运行的命令,只有当你手动终止它时才会返回错误码,根本无法实时捕捉超时情况。我们需要逐行捕获Ping的输出内容,并判断每一行是否包含超时关键字,再输出对应的内容。修改后的完整脚本如下:

@ECHO OFF
:: 保持窗口置顶的命令(保留你的实用功能,修正了原脚本中参数笔误:xy改为cy)
@powershell -ExecutionPolicy UnRestricted -Command "(Add-Type -memberDefinition \"[DllImport(\"\"user32.dll\"\")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,int y,int cx, int cy, uint flagsw);\" -name \"Win32SetWindowPos\" -passThru )::SetWindowPos((Add-Type -memberDefinition \"[DllImport(\"\"Kernel32.dll\"\")] public static extern IntPtr GetConsoleWindow();\" -name \"Win32GetConsoleWindow\" -passThru )::GetConsoleWindow(),-1,0,0,0,0,67)"
title Ping tool
mode con: cols=50 lines=25

:PING
cls
set "address="
set /p address= [*] Host to ping:
:: 防止用户直接回车导致Ping命令出错
if not defined address goto PING

@echo [*] Started pinging at: %time%
title Pinging %address%
@echo Pinging %address% with 32 bytes of data:

:: 逐行捕获Ping输出并过滤处理
for /f "delims=" %%a in ('ping %address% -t') do (
    :: 检查当前行是否为超时提示
    echo %%a | findstr /i "Request timed out" >nul
    if errorlevel 1 (
        :: 不是超时行,直接输出(排除重复的初始提示行)
        echo %%a | findstr /i "Pinging.*with 32 bytes of data:" >nul || echo %%a
    ) else (
        :: 是超时行,输出自定义提示
        echo Host didn't respond.
    )
)

echo. & pause & goto :PING

关键部分解释

  1. 逐行捕获输出:使用for /f "delims=" %%a in ('ping %address% -t')实时获取Ping命令的每一行输出,delims=确保完整保留包含空格的行内容。
  2. 关键字匹配:通过findstr /i "Request timed out"检查当前行是否为超时提示,/i参数兼容不同语言版本的Ping输出(比如部分版本可能显示英文大小写变化)。
  3. 输出逻辑控制:
    • 若不是超时行,直接输出原内容(额外过滤了重复的初始提示行,避免重复显示);
    • 若是超时行,替换为你需要的Host didn't respond.。
  4. 输入校验:增加了if not defined address goto PING,防止用户直接回车导致Ping命令执行出错。

效果验证

运行修改后的脚本,输入目标主机后,当出现超时时,会自动输出你期望的效果:

[*] Host to ping: 8.8.8.8
[*] Started pinging at: 13:44:29.05
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=28ms TTL=52
Reply from 8.8.8.8: bytes=32 time=16ms TTL=52
Host didn't respond.
Host didn't respond.

内容的提问来源于stack exchange,提问作者Buster

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:47:38