ping IP 带时间戳循环显示并写入日志

在工作中,判断网络是否通畅,首选命令就是ping,但有时候我们需要持续ping一个或多个地址时,需要加 -t 即可,但有时候需要在ping的时候加入时间戳并把ping记录写入到日志里面,方法如下:

windos版:

首选把下面代码复制到文本里去,然后把扩展名更改为.bat

@echo off
@echo.----------------------------------------------------------
@echo.       一   Author: aゞ锦衣卫
@echo.       键   Reminder:请以管理员身份运行
@echo.       ★   Description:一键ping+时间戳+写日志服务
@echo.       服   Blog:www.cnblogs.com/su-root
@echo.       务   Email:1147076062@qq.com VX:zikun868686
@echo.-----------------------------------------------------------
@echo.  ※温馨提醒:终止执行请按: Ctrl+C
@echo.-----------------------------------------------------------
@echo off
set /p host=请输入需要检测的IP地址:
set logfile=Log_%host%.log
echo Target Host = %host% >%logfile%
for /f "tokens=*" %%A in (‘ping %host% -n 1 ‘) do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in (‘ping %host% -n 1 ‘) do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL
    GOTO Ping)

运行.bat文件效果如下:

注:.bat文件放到哪里执行,就会在本地生成相应的.log日志文件。

我们打开日志文件看看:

 如果我们需要检测某IP地址的指定端口可将上面代码稍加改动即可:

@echo off
@echo.----------------------------------------------------------
@echo.       一   Author: aゞ锦衣卫
@echo.       键   Reminder:请以管理员身份运行
@echo.       ★   Description:一键端口检测服务
@echo.       服   Blog:www.cnblogs.com/su-root
@echo.       务   Email:1147076062@qq.com VX:zikun868686
@echo.-----------------------------------------------------------
@echo.  ※温馨提醒:终止执行请按: Ctrl+C
@echo.-----------------------------------------------------------
@echo off
set /p host=请输入需要检测的IP地址:
set /p port=请输入需要检测的端口号:
set logfile=Log_%host%.log
echo Target Host = %host% >>%logfile%
for /f "tokens=*" %%A in (‘tcping -d -t -n 1 %host% %port%‘) do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in (‘tcping -d -t -n 1 %host% %port%‘) do (
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
    echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
    timeout 1 >NUL
    GOTO Ping)

执行效果如下:

注:去官网下载tcping工具(根据自身系统选择32位/64位)https://elifulkerson.com/projects/tcping.php   tcping工具具体用法可参看:https://www.cnblogs.com/su-root/p/10924758.html

我们打开日志文件看看:

linux版:

[[email protected]118 ~]# ping 192.168.0.117|awk ‘{print strftime("%c",systime()) "\t"$0}‘
2019年07月04日 星期四 23时14分35秒    PING 192.168.0.117 (192.168.0.117) 56(84) bytes of data.
2019年07月04日 星期四 23时14分35秒    64 bytes from 192.168.0.117: icmp_seq=1 ttl=64 time=0.223 ms
2019年07月04日 星期四 23时14分36秒    64 bytes from 192.168.0.117: icmp_seq=2 ttl=64 time=0.385 ms
2019年07月04日 星期四 23时14分37秒    64 bytes from 192.168.0.117: icmp_seq=3 ttl=64 time=0.420 ms
2019年07月04日 星期四 23时14分38秒    64 bytes from 192.168.0.117: icmp_seq=4 ttl=64 time=0.291 ms
2019年07月04日 星期四 23时14分39秒    64 bytes from 192.168.0.117: icmp_seq=5 ttl=64 time=1.21 ms
2019年07月04日 星期四 23时14分40秒    64 bytes from 192.168.0.117: icmp_seq=6 ttl=64 time=1.45 ms

把输出信息写入到log日志中:

[[email protected]118 ~]# ping 192.168.0.117 -c 6|awk ‘{print strftime("%c",systime()) "\t"$0}‘ >ping.log

[[email protected]-118 ~]# cat ping.log
2019年07月04日 星期四 23时15分06秒    PING 192.168.0.117 (192.168.0.117) 56(84) bytes of data.
2019年07月04日 星期四 23时15分06秒    64 bytes from 192.168.0.117: icmp_seq=1 ttl=64 time=0.231 ms
2019年07月04日 星期四 23时15分07秒    64 bytes from 192.168.0.117: icmp_seq=2 ttl=64 time=0.331 ms
2019年07月04日 星期四 23时15分08秒    64 bytes from 192.168.0.117: icmp_seq=3 ttl=64 time=0.185 ms
2019年07月04日 星期四 23时15分09秒    64 bytes from 192.168.0.117: icmp_seq=4 ttl=64 time=0.347 ms
2019年07月04日 星期四 23时15分10秒    64 bytes from 192.168.0.117: icmp_seq=5 ttl=64 time=0.259 ms
2019年07月04日 星期四 23时15分11秒    64 bytes from 192.168.0.117: icmp_seq=6 ttl=64 time=0.377 ms
2019年07月04日 星期四 23时15分11秒
2019年07月04日 星期四 23时15分11秒    --- 192.168.0.117 ping statistics ---
2019年07月04日 星期四 23时15分11秒    6 packets transmitted, 6 received, 0% packet loss, time 5038ms
2019年07月04日 星期四 23时15分11秒    rtt min/avg/max/mdev = 0.185/0.288/0.377/0.069 ms

我们也可把任务放到后台运行

[[email protected]118 ~]# ping 192.168.0.117 -c 6|awk ‘{print strftime("%c",systime()) "\t"$0}‘ >ping.log &
[1] 1560
[[email protected]-118 ~]# 

当然也有其他方法检测,以上方法不是唯一的。

原文地址:https://www.cnblogs.com/su-root/p/11135458.html

时间: 2024-08-30 12:08:40

ping IP 带时间戳循环显示并写入日志的相关文章

Tools:实现ping操作带时间戳【windows+linux】

[windows下]: ping.vbs Dim args, flag, unsuccOut args="" otherout="" flag=0 If WScript.Arguments.count = 0 Then WScript.Echo "Usage: cscript tping.vbs [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]" WScript.Echo " [

ping域名和ping IP时速度不同的原因

不知道大家在ping的时候有没有遇到过这样的问题:当你ping一个域名的时候,ping结果返回得很慢,但是如果直接ping这个域名的ip,结果却快很多. 直接ping ip的时候,每两次发包之间没有明显的能感知出来的延迟: PING www.example.com (xxx.xxx.xxx.xxx) 56(84) bytes of data. 64 bytes from xxx.xxx.xxx.xxx: icmp_req=1 ttl=50 time=3.71 ms 64 bytes from x

批量Ping IP

刚刚接触Python 想做点什么 听说Python 在网络方便很厉害 后来总结如下: 第一:发现公司都固定IP 每次新来同事都要猜一个没有人用的IP  很费劲 第二:我们公司有的IP可以上QQ 有的不可以所以我每次也要换IP O(∩_∩)O 所以想到用Python 做一个批量Ping IP的工具 以至于方便于自 方便于人 少说多做  先上图 原理很简单 什么语言都可以实现的  献丑了 上代码 1 import subprocess 2 import string 3 import os 4 im

批量ping IP地址命令测试是否通

批量ping IP地址命令测试是否通 #!/bin/bash#--------------------------------------------------------------------------------------------------echo -e " "echo -e " ==============================================="echo -e "| Thanks for using this

WinForm LED循环显示信息,使用定时器Threading.Timer

原文:WinForm LED循环显示信息,使用定时器Threading.Timer 这里用一个示例来演示timer如何使用.示例:LED屏幕显示描述:这个示例其实很简单,LED屏幕上显示3个信息:        1:排队叫号         2:催缴费         3:等待列表.因为LED屏幕大小的关系,列表需要分页显示. 正常情况下,这3个信息都需要从服务器上去获得,这里的示例只做简单的模拟, 界面很简单,如图,这里我就不美化了. Timer构造函数参数说明: Callback:一个 Ti

Android开发实践:自定义带消息循环(Looper)的工作线程

上一篇文章提到了Android系统的UI线程是一种带消息循环(Looper)机制的线程,同时Android也提供了封装有消息循环(Looper)的HandlerThread类,这种线程,可以绑定Handler()对象,并通过Handler的sendMessage()函数向线程发送消息,通过handleMessage()函数,处理线程接收到的消息.这么说比较抽象,那么,本文就利用基础的Java类库,实现一个带消息循环(Looper)的线程,以帮助初学者理解这样一个Looper到底是怎么工作的. 1

Asp.net实现同页面内多图片自动上传并带预览显示

FileUpload控件实现单按钮图片自动上传并带预览显示 1.实现原理: 此方法适合针对有后台生成的图片相关内容,例如购物网站商品展示页面中的封面图片,图片的数量由后台访问数据库,并加载到页面.这种情况下,将会有x个FileUpload控件调用__doPostBack方法来用LinkButton的OnClick事件模拟一个事件触发的过程,由于上传控件的数量不固定,不可能使用多个用LinkButton的OnClick事件模拟事件触发的过程.也就是说只能有多个FileUpload控件调用一个Lin

background: url 背景图片加时间戳不显示图片

在项目中一段这样的代码 背景图片加时间戳图片显示不出来 <div id="header" class="header clearfix" style=" background: url(/U_HomeImage/T_CityManage/4_CM_Pic.png?2014-8-4 16:46:51) no-repeat left;background-size: 180px 87px;padding-left: 190px;"> 图片

javascript 循环显示&lt;hn&gt;字体

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>使用匿名函数</title> <script type="text/javascript"> var showFun=functi