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