微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux-Ping摘要未在bash脚本中显示日期

我正在尝试编写一个小脚本,该脚本将通过ping跟踪网络延迟.

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

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

#!/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

标准输出结果

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

文件结果

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

当我将日期添加到脚本时,文件结果从不显示统计信息
带时间戳的脚本

#!/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

标准输出结果

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

文件结果

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

谢谢大家的指导.

解决方法:

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

对于内置的陷阱来说,这似乎是一项工作.

调整原始脚本:

#!/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手册页的SIGNALS部分中:bash运行的非内置命令将信号处理程序设置为shell从其父级继承的值.

上面的trap命令意味着运行while循环的shell不会响应键盘中断(信号INT),但是非内置ping命令将在bash启动它时将其配置重置为认值,因此会被键盘中断信号.然后ping发出其摘要退出,shell幸存下来以捕获所有该输出.

您还可以构建事物,以免依赖于后台进程/尾部组合:

#!/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选项来提示输入.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐