首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ping摘要在bash脚本中没有显示日期

ping摘要在bash脚本中没有显示日期
EN

Stack Overflow用户
提问于 2017-08-04 18:35:14
回答 2查看 825关注 0票数 3

我正在尝试编写一个小脚本,它将跟踪ping的网络延迟。

我需要写入一个文件,并用日期和时间标记每个ping条目。我需要实时看到响应,如果ping时间太长,我需要停止脚本。

我可以在没有日期的文件中获得ping结果和摘要,代码如下

代码语言:javascript
复制
#!/usr/bin/env bash

echo "Enter Dealer number: "
read deal
echo "Enter IP address: "
read ip
touch ./${deal}_pingtest.txt
ping $ip > ./${deal}_pingtest.txt &
tail -f ./${deal}_pingtest.txt

标准结果

代码语言:javascript
复制
Enter Dealer number:
test
Enter IP address:
8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.87 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.36 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.30 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=4.48 ms
^C

档案结果

代码语言:javascript
复制
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.87 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=5.36 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=8.30 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=4.48 ms

--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 4.488/5.758/8.309/1.506 ms

当我将日期添加到脚本中时,文件结果将不会显示带有时间戳的stats脚本。

代码语言:javascript
复制
#!/usr/bin/env bash

echo "Enter Dealer number: "
read deal
echo "Enter IP address: "
read ip
touch ./${deal}_pingtest.txt
ping $ip | while read pong; do echo "$(date +%Y-%m-%d\|%H:%M:%S): $pong"; done > ./${deal}_pingtest.txt &
tail -f ./${deal}_pingtest.txt

标准结果

代码语言:javascript
复制
Enter Dealer number:
test
Enter IP address:
8.8.8.8
2017-08-04|11:31:29: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2017-08-04|11:31:29: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.71 ms
2017-08-04|11:31:30: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=4.53 ms
2017-08-04|11:31:31: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=4.85 ms
2017-08-04|11:31:32: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=5.11 ms
2017-08-04|11:31:33: 64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=4.51 ms
^C

档案结果

代码语言:javascript
复制
2017-08-04|11:31:29: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
2017-08-04|11:31:29: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=58 time=4.71 ms
2017-08-04|11:31:30: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=58 time=4.53 ms
2017-08-04|11:31:31: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=58 time=4.85 ms
2017-08-04|11:31:32: 64 bytes from 8.8.8.8: icmp_seq=4 ttl=58 time=5.11 ms
2017-08-04|11:31:33: 64 bytes from 8.8.8.8: icmp_seq=5 ttl=58 time=4.51 ms
2017-08-04|11:31:34: 64 bytes from 8.8.8.8: icmp_seq=6 ttl=58 time=4.89 ms

谢谢各位的指导。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-04 20:16:04

我想您是在使用键盘上的ctrl-C来中断这个脚本。您需要编写代码,以便ping命令被中断并发布其摘要信息,但是捕获ping输出的shell仍然能够捕获该摘要并将其发送到输出文件。

这似乎是trap内置于bash的一项工作。

调整原始脚本:

代码语言:javascript
复制
#!/usr/bin/env bash

read -p "Enter Dealer number: " deal
read -p "Enter IP address: " ip

trap INT
ping $ip | while read pong; do echo "$(date +%Y-%m-%d\|%H:%M:%S): $pong"; done > "$deal"_pingtest.txt &
tail -f "$deal"_pingtest.txt

从bash手册页的信号部分:bash运行的非内置命令将信号处理程序设置为shell从其父节点继承的值。

上面的trap命令意味着运行while循环的shell不会响应键盘中断(信号INT),但非内置ping命令将在bash启动时将其配置重置为默认设置,因此将被键盘信号中断。然后,ping发出总结并退出,外壳保存下来捕获所有的输出。

您还可以构造一些东西,这样就不会依赖于后台进程/ tail组合:

代码语言:javascript
复制
#!/usr/bin/env bash

read -p 'Enter Dealer number: ' deal
read -p 'Enter IP address: ' ip

trap '' INT

ping "$ip" |
while read pong; do
  echo "$(date '+%Y-%m-%d|%H:%M:%S'): $pong"
done | tee "$deal"_pingtest.txt

请注意,在这两种情况下,我都使用read内置和它的-p选项来提示输入。

票数 2
EN

Stack Overflow用户

发布于 2017-08-04 20:10:30

只有当使用pingSIGINTSIGQUIT时,才会显示统计数据(或者达到-c count定义的页面数,但您没有使用它)。来自man ping

当已发送(和接收)指定数量的数据包时,或者如果程序使用SIGINT终止,则显示一个简短的摘要。利用信号SIGQUIT可以在不终止处理的情况下获得较短的电流统计量。

因此,如果要打印统计数据,请确保像这样杀死ping

代码语言:javascript
复制
pkill ping -SIGINT
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45513466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档