ping 加上時間信息


1、命令行打印並加上時間信息

[root@test ~]# ping www.baidu.com | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime())}'  
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.    2020-05-11 15:07:12
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=1.18 ms    2020-05-11 15:07:12
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=1.32 ms    2020-05-11 15:07:13
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=1.55 ms    2020-05-11 15:07:14
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=1.25 ms    2020-05-11 15:07:15
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=1.25 ms    2020-05-11 15:07:16
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=6 ttl=55 time=1.18 ms    2020-05-11 15:07:17

2、將這些信息打印到一個文件中

[root@test ~]# nohup ping www.baidu.com | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()); fflush()}' >> outIP.info &  
[1] 16368
上面命令表示后台執行命令,但退出終端后會停止運行,如想保持長期執行命令需要加上參數nohup
注意:使用  fflush() ,不然文件不會有信息,因為awk也是有緩存的。
[root@test ~]# jobs  #查看后台運行的任務
[1]+  Running                 ping www.baidu.com | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()); fflush()}' >> outIP.info &
[root@test ~]# fg 1  #把任務放在前台運行
ping www.baidu.com | awk '{ print $0"\t" strftime("%Y-%m-%d %H:%M:%S",systime()); fflush()}' >> outIP.info

3、另外提供下利用ping命令檢測網絡中主機是否在線;

#!/bin/bash
i=10
  for j in {10..254};do
     if ping -c 1 -w 1 192.168.$i.$j &> /dev/null;then
        echo -e "\033[32m192.168.$i.$j\033[0m is up"    #此處字體以綠色顯示;
     else
        echo -e "\033[31m192.168.$i.$j\033[0m is down"  #此處字體以紅色顯示;
     fi
  done


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM