ping IP 带时间戳循环显示并写入日志(windos版+linux版)

在工作中,判断网络是否通畅,首选命令就是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版:

[root@bqh-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日志中:

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

[root@bqh-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

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

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

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

转载于:https://www.cnblogs.com/su-root/p/11135458.html

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值