批量ping并保存结果到TXT中

有时候我们需要批量ping一些IP地址测试连通性,或是ping IP解析为电脑名、将电脑名解析为IP、又或是测试端口连通性。

1.txt是需要Ping的电脑名或是ip地址

2.txt是结果输出

@echo off
:: 设置代码页为 UTF-8,避免中文乱码(适用于 Win10 及以上)
chcp 65001 >nul

:: 定义输入和输出文件
set input=1.log
set output=2.txt

:: 清空输出文件
echo. > "%output%"

:: 检查输入文件是否存在
if not exist "%input%" (
    echo 错误:找不到文件 %input%
    echo 请确保该文件与本脚本位于同一目录。
    echo.
    pause
    exit /b
)

echo 正在批量 Ping 主机,请稍候...
echo.

:: 开始读取每行并执行ping命令
for /f "tokens=*" %%a in (%input%) do (
    echo 正在 Ping: %%a ...
    echo -------------------------------------------------- >> "%output%"
    echo Pinging %%a >> "%output%"
    echo -------------------------------------------------- >> "%output%"
    :: 执行Ping命令(4次,每次最多等待500ms)
    ping %%a -n 2 -w 500 >> "%output%"
    
    echo. >> "%output%"
    echo. >> "%output%"
)

echo.
echo 所有Ping测试已完成。
echo 结果已保存至:%output%
echo.
pause

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值