zoukankan      html  css  js  c++  java
  • 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()) "	"$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()) "	"$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()) "	"$0}' >ping.log &
    [1] 1560
    [root@bqh-118 ~]# 

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

  • 相关阅读:
    测试EF6.1.3和OrmLite性能
    MVC自定义分页(附表跳转页Loading提示)
    仿京东手机充值进度导航
    PTA(Advanced Level)1052.Linked List Sorting.
    用双指针法巧解——PTA(Advanced Level)1032.Sharing
    PTA(Advanced Level)1022.Digital Library
    PTA(Advanced Level)1071.Speech Patterns
    PTA(Advanced Level)1054.The Dominant Color
    PTA(Advanced Level)1100.Mars Numbers
    PTA(Advanced Level)1060.Are They Equal
  • 原文地址:https://www.cnblogs.com/su-root/p/11135458.html
Copyright © 2011-2022 走看看