批处理脚本 - 平地址 - 写入文件,如果失败 [英] Batch Script - Ping Address - Write to file if failure

查看:152
本文介绍了批处理脚本 - 平地址 - 写入文件,如果失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要ping IP地址每5秒。

如果ping不通,写上日期和时间的文件。

下面是我的非工作尝试...循环如预期运作,但我不能得到它,如果ping失败写入文件。

  @ECHO OFF
设置IPADDRESS = 172.30.1.36
设定的时间间隔= 5
:PINGINTERVAL平%IPADDRESS%-n 1
如果ERRORLEVEL 1回音%日期%%时间%GT;> failurelog.txt超时间隔%%
GOTO PINGINTERVAL


解决方案

在IPv4中,如果有丢失的数据包ping命令只能引发错误级别​​。但是,在你在你的同一子网执行ping一台机器,你没有丢失的数据包。

要测试的ping成功的更简单的方法是在平的输出

测试对于TTL =字符串

 平-n 1%ip地址%|找到TTL => NUL
如果ERRORLEVEL 1回音%日期%%时间%GT;> failurelog.txt

I want to ping an IP address every 5 seconds.

If the ping fails, write the date and time to a file.

Here is my non-working attempt... the loop works as intended, but I can't get it to write to a file if the ping fails.

@ECHO OFF
set IPADDRESS=172.30.1.36
set INTERVAL=5
:PINGINTERVAL

ping %IPADDRESS% -n 1
if errorlevel 1 echo %date% %time% >> failurelog.txt

timeout %INTERVAL%
GOTO PINGINTERVAL

解决方案

In ipv4, ping command only raises errorlevel if there are packets lost. But in you are pinging a machine in your same subnet, you get no packets lost.

The easier way to test for ping success is to test for the "TTL=" string in the output of the ping

ping -n 1 %ipaddress% | find "TTL=" > nul
if errorlevel 1 echo %date% %time% >> failurelog.txt

这篇关于批处理脚本 - 平地址 - 写入文件,如果失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆