在工作中,判斷網絡是否通暢,首選命令就是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 ~]#
當然也有其他方法檢測,以上方法不是唯一的。