连接失败时如何打破循环?

时间:2013-08-27 12:40:43

标签: batch-file ping

我正在研究ping批处理程序。 我想学的是如何在连接断开时跳过循环? 举个例子:

@echo off
:1
ping www.stackoverflow.com
(I guess) if errorlevel 1 set errorlev=1
If errorlevel 1 goto 2
goto 1
:2
Echo ping failed!
Title ping failed!
color c

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

:back
ping www.stackoverflow.com >nul && goto:back
Echo ping failed!

答案 1 :(得分:0)

所以你想永远ping,直到ping失败,然后停止?

@echo off
:1
ping -n 1 www.stackoverflow.com | find "Reply from">nul2>nul && goto :1
echo ping failed!
title ping failed!
color c

注意:我试图找到而不是检查ERRORLEVEL,因为某些Windows版本与某些故障条件设置ERRORLEVEL不一致。请参阅以下示例。

C:\Users\Me>ping -n 1 192.168.1.2

Pinging 192.168.1.2 with 32 bytes of data:
Reply from 192.168.1.102: Destination host unreachable.

Ping statistics for 192.168.1.2:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

C:\Users\Me>echo %errorlevel%
0