免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3569 | 回复: 5
打印 上一主题 下一主题

ping结果中加时间的shell脚本如何编写呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-13 12:27 |只看该作者 |倒序浏览
下面是ping命令:

#  ping -s -I 1 192.168.200.158           
PING 192.168.200.158: 56 data bytes
64 bytes from 192.168.200.158: icmp_seq=0. time=2. ms
64 bytes from 192.168.200.158: icmp_seq=1. time=1. ms
64 bytes from 192.168.200.158: icmp_seq=2. time=1. ms
64 bytes from 192.168.200.158: icmp_seq=3. time=1. ms

----192.168.200.158 PING Statistics----
11 packets transmitted, 11 packets received, 0% packet loss
round-trip (ms)  min/avg/max = 1/1/2

要求结果是这样的:
2006-12-13 12:25:01  64 bytes from 192.168.200.158: icmp_seq=0. time=2. ms
2006-12-13 12:25:02  64 bytes from 192.168.200.158: icmp_seq=1. time=1. ms
2006-12-13 12:25:03  64 bytes from 192.168.200.158: icmp_seq=2. time=1. ms
2006-12-13 12:25:04  64 bytes from 192.168.200.158: icmp_seq=3. time=1. ms

这个shell脚本写如何下手呀,请高手多指点,谢谢! 急

[ 本帖最后由 bjtubin 于 2006-12-13 13:38 编辑 ]

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2006-12-13 12:52 |只看该作者
sed "s/^/$(date) /" ur-file

论坛徽章:
0
3 [报告]
发表于 2007-01-13 14:52 |只看该作者
我写的一个脚本:

while [ true ]
do
  date=`date +%D`
  time=`date +%T`
  echo $date $time  "\c"
  ping -c 1 10.194.83.121 |grep "bytes from"
  sleep 1
done

论坛徽章:
0
4 [报告]
发表于 2007-01-13 15:39 |只看该作者
原帖由 waker 于 2006-12-13 12:52 发表
sed "s/^/$(date) /" ur-file


这个方法好
使用默认或指定ping包的大小
[root@seker shell]# ping www.google.com
PING www-china.l.google.com (66.249.89.99) 56(84) bytes of data.
64 bytes from jp-in-f99.google.com (66.249.89.99): icmp_seq=0 ttl=244 time=211 ms
64 bytes from jp-in-f99.google.com (66.249.89.99): icmp_seq=1 ttl=244 time=213 ms

[root@seker shell]# ping www.google.com |sed "s/64/$(date) 64/g"
PING www-china.l.google.com (66.249.89.99) 56(84) bytes of data.
六  1月 13 15:44:52 CST 2007 64 bytes from jp-in-f99.google.com (66.249.89.99): icmp_seq=0 ttl=244 time=194 ms
六  1月 13 15:44:52 CST 2007 64 bytes from jp-in-f99.google.com (66.249.89.99): icmp_seq=1 ttl=244 time=193 ms

论坛徽章:
0
5 [报告]
发表于 2007-01-13 17:30 |只看该作者
day=`date "+%Y-%m-%d"`
awk -vdate=$day 'BEGIN{NR=2}NR<58{print date,NR-1,$0}'

[ 本帖最后由 zsk203 于 2007-1-13 17:47 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-01-13 18:22 |只看该作者
[root@seker shell]# ./pingdate.sh
输入ping的次数:3
输入被ping的IP:192.168.1.1
2007-01-13 18:28: 1 64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=0.175 ms
2007-01-13 18:28: 2 64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=0.154 ms
2007-01-13 18:28: 3 64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=0.174 ms

[root@seker shell]# more pingdate.sh
#!/bin/bash
#ping + date
echo -n "输入ping的次数:"
read CONT
echo -n "输入被ping的IP:"
read IPADD
i=1
while [ $i -le $CONT ]
do
ping -c 1 -l 64 $IPADD |sed -n "s/64/$(date --iso-8601) $(date |awk '{print $4}'|awk -F: '{print
$1":"$2}'): $i 64/p"
i=`expr $i + 1`
done

[root@seker shell]#

$CONT、$IPADD的正确输入再加上个检测就OK了
不知道怎么检测数字和IP地址的合法性

[ 本帖最后由 bleach 于 2007-1-13 18:46 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP