在工作中,判断网络是否通畅,首选命令就是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:@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:~,%:%time:~,%:%time:~,% %%A>>%logfile%
echo %date% %time:~,%:%time:~,%:%time:~,% %%A
timeout >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:@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:~,%:%time:~,%:%time:~,% %%A>>%logfile%
echo %date% %time:~,%:%time:~,%:%time:~,% %%A
timeout >NUL
GOTO Ping)

执行效果如下:

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

我们打开日志文件看看:

linux版:

[root@bqh- ~]# 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) () bytes of data.
2019年07月04日 星期四 23时14分35秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.223 ms
2019年07月04日 星期四 23时14分36秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.385 ms
2019年07月04日 星期四 23时14分37秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.420 ms
2019年07月04日 星期四 23时14分38秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.291 ms
2019年07月04日 星期四 23时14分39秒 bytes from 192.168.0.117: icmp_seq= ttl= time=1.21 ms
2019年07月04日 星期四 23时14分40秒 bytes from 192.168.0.117: icmp_seq= ttl= time=1.45 ms

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

[root@bqh- ~]# ping 192.168.0.117 -c |awk '{print strftime("%c",systime()) "\t"$0}' >ping.log

[root@bqh- ~]# cat ping.log
2019年07月04日 星期四 23时15分06秒 PING 192.168.0.117 (192.168.0.117) () bytes of data.
2019年07月04日 星期四 23时15分06秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.231 ms
2019年07月04日 星期四 23时15分07秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.331 ms
2019年07月04日 星期四 23时15分08秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.185 ms
2019年07月04日 星期四 23时15分09秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.347 ms
2019年07月04日 星期四 23时15分10秒 bytes from 192.168.0.117: icmp_seq= ttl= time=0.259 ms
2019年07月04日 星期四 23时15分11秒 bytes from 192.168.0.117: icmp_seq= ttl= 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秒 packets transmitted, received, % packet loss, time 5038ms
2019年07月04日 星期四 23时15分11秒 rtt min/avg/max/mdev = 0.185/0.288/0.377/0.069 ms

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

[root@bqh- ~]# ping 192.168.0.117 -c |awk '{print strftime("%c",systime()) "\t"$0}' >ping.log &
[]
[root@bqh- ~]#

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

ping IP 带时间戳循环显示并写入日志(windos版+linux版)的更多相关文章

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

    [windows下]: ping.vbs Dim args, flag, unsuccOut args="" otherout="" flag= If WScr ...

  2. 长ping域名带时间戳

    ping www.baidu.com |awk '{print $0 "\t" strftime("%Y:%m:%d-%H:%M:%S",systime())} ...

  3. 串口助手下载-带时间戳的串口助手-极简串口助手-V1.1 自动保存配置参数 能显示收发时间方便调试

    1.串口助手下载 2.带时间戳的串口助手,每次收发指令带上了时间戳,方便调试 3.极简串口助手 4.简单易用 高速稳定 5.每次修改的参数都能自动保存,免去了重复配置的工作 下载地址:http://w ...

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

    不知道大家在ping的时候有没有遇到过这样的问题:当你ping一个域名的时候,ping结果返回得很慢,但是如果直接ping这个域名的ip,结果却快很多. 直接ping ip的时候,每两次发包之间没有明 ...

  5. 批量ping IP并检测IP延迟率和丢包率脚本

    脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...

  6. 批量Ping IP

    刚刚接触Python 想做点什么 听说Python 在网络方便很厉害 后来总结如下: 第一:发现公司都固定IP 每次新来同事都要猜一个没有人用的IP  很费劲 第二:我们公司有的IP可以上QQ 有的不 ...

  7. wxPython制作跑monkey工具(python3)-带事件百分比显示界面

    一. wxPython制作跑monkey工具(python3)-带事件百分比显示界面  源代码 Run Monkey.py #!/usr/bin/env python import wx import ...

  8. shell 编写脚本批量Ping IP

    服务器总是一下子买了很多的段的ip.通过绑定后,也不知道这些ip是否绑定成功,所以就写了一个shell脚本,把ip输好,批量ping一下,看是不是都能ping通. 脚本如下: 此外.还有一个ip文件, ...

  9. Linux下长时间ping网络加时间戳并记录到文本

    Linux下长时间ping网络加时间戳并记录到文本   由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...

随机推荐

  1. C++构造函数以及何时被调用

    using namespace std; class A { public: A() { cout << "默认无参构造函数" << endl; } #if ...

  2. ANSI转义序列

    http://ascii-table.com/ansi-escape-sequences.php 制作控制台程序时, 要实现一些特殊效果, 需要了解一下 [ANSI转义序列] . ANSI转义序列是一 ...

  3. 123457123456#2#----com.MCgame.ShuXueKoSuan98--前拼后广--儿童小学数学口算Game-mc22222

    com.MCgame.ShuXueKoSuan98--前拼后广--儿童小学数学口算Game-mc

  4. html如何修改hr水平直线的粗细

    hr是常见的超文本标签,是一条水平直线,要设置该直线变粗一些.可以先把hr本身的border隐藏掉,然后设置border-top-width,也就是只留上边框,如图:hr的默认高度height是0,所 ...

  5. css样式圆角和一定的透明度

    css样式里可以用border-radius把div或图片变成带有一定圆角的,如果是div本身是正方形,设置圆角度是百分之五十,就是圆形. border-radius:值可以是具体的px数值,也可以是 ...

  6. Python3之内建模块base64

    Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...

  7. ajax页面刷新小错误(提交按钮type必须为button,而不能是submit)

    背景: 使用ajax提交form表单时,提交按钮的type值写为了submit,导致ajax中回调函数中的提示信息toastr.success('提交数据成功');没有执行,只执行了alert语句 , ...

  8. 【VS开发】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)

    1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...

  9. vue 跨域简记

    0.服务端设置 app.use(function(req, res, next){ //设置跨域访问 res.header('Access-Control-Allow-Origin', '*'); r ...

  10. Django_图片的上传下载显示配置

    图片上传的配置 image = models.ImageField(upload_to='org/%Y/%m',...) upload_to默认是上传到项目的'MEDIA_ROOT/org/%Y/%m ...