有时候我们需要批量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