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:@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版)的更多相关文章
- Tools:实现ping操作带时间戳【windows+linux】
[windows下]: ping.vbs Dim args, flag, unsuccOut args="" otherout="" flag= If WScr ...
- 长ping域名带时间戳
ping www.baidu.com |awk '{print $0 "\t" strftime("%Y:%m:%d-%H:%M:%S",systime())} ...
- 串口助手下载-带时间戳的串口助手-极简串口助手-V1.1 自动保存配置参数 能显示收发时间方便调试
1.串口助手下载 2.带时间戳的串口助手,每次收发指令带上了时间戳,方便调试 3.极简串口助手 4.简单易用 高速稳定 5.每次修改的参数都能自动保存,免去了重复配置的工作 下载地址:http://w ...
- ping域名和ping IP时速度不同的原因
不知道大家在ping的时候有没有遇到过这样的问题:当你ping一个域名的时候,ping结果返回得很慢,但是如果直接ping这个域名的ip,结果却快很多. 直接ping ip的时候,每两次发包之间没有明 ...
- 批量ping IP并检测IP延迟率和丢包率脚本
脚本文件如下: #!/bin/bash #Author:Mr.Ding #Created Time:2018-08-26 07:23:44 #Name:ping.sh #Description: sh ...
- 批量Ping IP
刚刚接触Python 想做点什么 听说Python 在网络方便很厉害 后来总结如下: 第一:发现公司都固定IP 每次新来同事都要猜一个没有人用的IP 很费劲 第二:我们公司有的IP可以上QQ 有的不 ...
- wxPython制作跑monkey工具(python3)-带事件百分比显示界面
一. wxPython制作跑monkey工具(python3)-带事件百分比显示界面 源代码 Run Monkey.py #!/usr/bin/env python import wx import ...
- shell 编写脚本批量Ping IP
服务器总是一下子买了很多的段的ip.通过绑定后,也不知道这些ip是否绑定成功,所以就写了一个shell脚本,把ip输好,批量ping一下,看是不是都能ping通. 脚本如下: 此外.还有一个ip文件, ...
- Linux下长时间ping网络加时间戳并记录到文本
Linux下长时间ping网络加时间戳并记录到文本 由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...
随机推荐
- jsp、freemarker、velocity、thymeleaf
1.概述在java领域,表现层技术主要有三种, (1)jsp; (2)freemarker; (3)velocity; (4)thymeleaf; 2.jsp优点: 1.功能强大,可以写java代码 ...
- xshell连接中标麒麟
中标麒麟: 1.首先肯定需要给虚拟机配置静态ip,让虚拟机作为独立的ip存在 https://www.cnblogs.com/judes/p/11776872.html 2.输入rpm -qa | g ...
- Python - Django - ORM Django 终端打印 SQL 语句
在 settings.py 中添加以下内容: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'c ...
- [转]Office 安装卸载太麻烦?用这个工具帮你解决:Office Tool Plus
原文链接:https://sspai.com/post/43839 Office Tool官方网站:https://otp.landian.vip/zh-cn/ 真的很好用,发一个安装的截图:
- 微信小程序 与后台交互----传递和回传时间
wxml代码 <!--index.wxml--> <view class="container"> <view class="section ...
- gluster设置日志级别
glusterd --log-level WARNING #将日志级别设定为warning gluster --log-level=ERROR volume status #查看日志级别的状态 glu ...
- start use webpack
Demo0操作手册 本Demo演示不使用配置文件的入门级使用 准备环境 初始化环境, cd到demo目录之后, 执行如下命令: npm init -y npm install webpack webp ...
- Mybatis笔记2
使用Mybatis完成的CRUD操作 个人总结的一些小规律 学习过程中碰到的错误: org.apache.ibatis.exceptions.PersistenceException: ### Err ...
- Python基础 — 八种数据类型
Python 3.x 的八种数据类型 八种数据类型分别是: number(数字).string(字符串).Boolean(布尔值).None(空值) list(列表).tuple(元组).dict(字 ...
- Mysql之rpm安装5.7版本遇见的问题
前言:环境是centos7.5的系统,用rpm方式安装mysql5.7 1.由于是centos7.5 所以需要将默认的mariadb给卸载 rpm -qa | grep mariadb 查看下是否有m ...