@rem ping.bat Createdby xiaoshancom
@rem (a) Unknown host = ERRORLEVEL 1
@rem (usually=name not found on DomainNameServer)
@rem (b) Brief help (with no parameter) = ERRORLEVEL 1
@rem Note: for PING Brief help with /? switch, ERRORLEVEL is 0
@rem (c) Invalid switch = ERRORLEVEL 1
@rem (and displays the Brief help as well)
@rem (d) Interrupted with [Ctrl-C] = ERRORLEVEL 255
@echo off
@以上是注释。下面才是程序。
echo start ping>>log1.txt
@开始运行ping命令。 (相当于在cmd下输入start ping)
date /t>>log1.txt
@写日期
time /t>>log1.txt
@写时间
:start
@start标记
ping -n 1 192.16.5.51|find "Reply"
@ping 目标,同时在返回结果中查找“Reply”,| 是管道,可以查找相关文档学习一下
if errorlevel 1 goto end
@如果是Unknown host 、Brief help (with no parameter)、Invalid switch 中的一个,则跳到:end。
if not errorlevel 1 goto start
@如果找到,或者按下ctrl+c,或者输入下/?(刚才漏了这个),则返回:start
:end
@结束标记
echo connection failed,current time is:>>log1.txt
@结束,把"connection failed,current time is:“写进log1.txt
date /t>>log1.txt
@再写日期
time /t>>log1.txt
@写时间
rem exit
exit
@结束批处理命令