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

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

最新文章

  1. Submit Text 快捷键总结
  2. cdn提供商
  3. Jquery禁止/恢复按钮与文本框代码
  4. 【读书笔记】iOS-开发技巧-UILabel内容模糊的原因
  5. linux编译ruby1.8.7 出现OPENSSL错误
  6. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)
  7. 顺序表的基本操作(C)
  8. 漫话C++0x(五)—- thread, mutex, condition_variable
  9. linux 调度器配制参数
  10. Docker集群实验环境布署--swarm【4 管理组件--manager】
  11. mkdirs自动创建文件夹
  12. 前端之bootstrap模态框
  13. 关于MySQL数据库的安装和卸载
  14. mysql权限参考
  15. 【Effective Java读书笔记】创建和销毁对象(一):考虑使用静态工厂方法代替构造器
  16. .netcore 写日志(使用NLog,log4net)
  17. const的引用
  18. 安装win10 1703版本操作系统
  19. BZOJ2671 Calc(莫比乌斯反演)
  20. Windows内置系统账户:Local system/Network service/Local Service 区别

热门文章

  1. 转:HR schema
  2. websehll的使用和预防措施
  3. 123457123456#0#----com.ppGame.ChengYu43--前拼后广--成语caicaicai_pp
  4. 解决软件卸载时Abstract: "Invalid serial number" xe4
  5. Linux下,postgreSQL的查看与重启
  6. Gulp-构建工具 相关内容整理
  7. C之指针加减运算
  8. 剑指offer66:机器人的运动范围
  9. js 自定义加减乘除方法(防止js自身计算错误)
  10. io.lettuce.core.protocol.ConnectionWatchdog - Reconnecting, last destination was ***