我正在尝试编写一个小脚本,它将跟踪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当我将日期添加到脚本中时,文件结果将不会显示带有时间戳的stats脚本。
#!/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谢谢各位的指导。
发布于 2017-08-04 20:16:04
我想您是在使用键盘上的ctrl-C来中断这个脚本。您需要编写代码,以便ping命令被中断并发布其摘要信息,但是捕获ping输出的shell仍然能够捕获该摘要并将其发送到输出文件。
这似乎是trap内置于bash的一项工作。
调整原始脚本:
#!/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组合:
#!/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选项来提示输入。
发布于 2017-08-04 20:10:30
只有当使用ping或SIGINT或SIGQUIT时,才会显示统计数据(或者达到-c count定义的页面数,但您没有使用它)。来自man ping
当已发送(和接收)指定数量的数据包时,或者如果程序使用SIGINT终止,则显示一个简短的摘要。利用信号SIGQUIT可以在不终止处理的情况下获得较短的电流统计量。
因此,如果要打印统计数据,请确保像这样杀死ping:
pkill ping -SIGINThttps://stackoverflow.com/questions/45513466
复制相似问题