无需停止即可检查 ping 统计信息

2ma*_*mac 39 linux command-line gnu ping

有没有办法在ping不停止执行的情况下告诉显示其通常的终止统计信息?

例如,我想快速查看:

--- 8.8.8.8 ping statistics ---
2410 packets transmitted, 2274 received, +27 errors, 5% packet loss, time 2412839ms
rtt min/avg/max/mdev = 26.103/48.917/639.493/52.093 ms, pipe 3
Run Code Online (Sandbox Code Playgroud)

无需停止程序,从而丢失累积的数据。

smh*_*ich 38

ping联机帮助页(强调我的):

当已发送(和接收)指定数量的数据包或程序以 SIGINT 终止时,将显示简短摘要。无需通过信号 SIGQUIT 终止进程即可获得更短的当前统计信息。

因此,如果您的统计数据稍微不那么冗长,这将起作用:

# the second part is only for showing you the PID
ping 8.8.8.8 & jobs ; fg

<... in another terminal ...>

kill -SIGQUIT $PID
Run Code Online (Sandbox Code Playgroud)

简短的统计数据如下所示:

19/19 packets, 0% loss, min/avg/ewma/max = 0.068/0.073/0.074/0.088 ms
Run Code Online (Sandbox Code Playgroud)

  • 一个小小的补充:你可以通过点击“Ctrl-\”从终端发出`SIGQUIT`,不需要打开第二个终端并使用`kill`。 (38认同)
  • 此外,在 BSD 变体(包括 OSX)上,ping 使用 SIGINFO 信号进行汇总,该信号可以在终端中使用 Ctrl-T 触发。 (8认同)
  • 我经常有多个终端持续不断的“ping”。我通常开始'while true; do ps -o "pid" -C "ping" h | xargs kill -SIGQUIT; 睡20;在开始`ping`之前在第一个终端完成&amp;`。这允许我开始新的`ping`s 和/或重新启动现有的`ping`s(以重置统计数据)并且不需要跟踪`PID`s 的`kill`。 (3认同)
  • 使用这种方法意味着不需要使用`&amp;jobs; 答案中命令的 fg` 部分,因为它的唯一目的是报告 `PID`... 抱歉补充评论 - 由于 5 分钟阈值,无法再编辑我的原始评论。 (2认同)
  • @StarsonH​​ochschild,我建议您将您的方法作为新答案发布。 (2认同)

小智 20

还有一种更简单的方法可以在执行期间获取 ping 统计信息:只需按Ctrl + | (垂直斜线或也称为管道线)

我个人确实经常使用它,试试看:

64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=0.893 ms
64 bytes from 192.168.1.1: icmp_seq=23 ttl=64 time=0.862 ms
64 bytes from 192.168.1.1: icmp_seq=24 ttl=64 time=3.18 ms
64 bytes from 192.168.1.1: icmp_seq=35 ttl=64 time=0.877 ms
64 bytes from 192.168.1.1: icmp_seq=36 ttl=64 time=0.866 ms
**36/36 packets, 0% loss, min/avg/ewma/max = 0.832/0.993/0.930/3.185 ms**
64 bytes from 192.168.1.1: icmp_seq=37 ttl=64 time=0.909 ms
64 bytes from 192.168.1.1: icmp_seq=38 ttl=64 time=2.03 ms
64 bytes from 192.168.1.1: icmp_seq=39 ttl=64 time=0.839 ms
64 bytes from 192.168.1.1: icmp_seq=40 ttl=64 time=0.880 ms
Run Code Online (Sandbox Code Playgroud)

  • 我很惊讶地发现,对于我在 Ubuntu Bionic 运行的终端中,根据 xev 的说法,“Ctrl + \” 与此处其他答案中提到的组合键相同:“Ctrl + |” 和“Ctrl + 4”。所有这些都会产生一个 0x1c 的 XLookupString,它绑定到 SIGQUIT,如“stty -a”所示。另请参阅[键盘快捷键 - Shell SIGKILL 键绑定 - 超级用户](https://superuser.com/questions/288772/shell-sigkill-keybinding) (3认同)
  • 在命运的转折中,在从选定的最佳答案中得到答案后,我实际上为该特定功能贡献了一些代码。 (2认同)

Sta*_*ild 10

Linux(在 Ubuntu 20.04 上测试)

发送SIGQUIT信号。

输出示例:

64 bytes from localhost (127.0.0.1): icmp_seq=138 ttl=64 time=0.021 ms
64 bytes from localhost (127.0.0.1): icmp_seq=139 ttl=64 time=0.022 ms
139/139 packets, 0% loss, min/avg/ewma/max = 0.014/0.022/0.022/0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=140 ttl=64 time=0.090 ms
64 bytes from localhost (127.0.0.1): icmp_seq=141 ttl=64 time=0.025 ms
Run Code Online (Sandbox Code Playgroud)

运行时发送

CTRL+ \=quit根据stty -a.
这些也有效:CTRL+ |; CTRL+ 4;

发送到一个或多个已知的 pid

64 bytes from localhost (127.0.0.1): icmp_seq=138 ttl=64 time=0.021 ms
64 bytes from localhost (127.0.0.1): icmp_seq=139 ttl=64 time=0.022 ms
139/139 packets, 0% loss, min/avg/ewma/max = 0.014/0.022/0.022/0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=140 ttl=64 time=0.090 ms
64 bytes from localhost (127.0.0.1): icmp_seq=141 ttl=64 time=0.025 ms
Run Code Online (Sandbox Code Playgroud)

发送给所有正在运行的人

kill -SIGQUIT <pid> [...]
Run Code Online (Sandbox Code Playgroud)

定期发送给所有正在运行的

ps -o pid= -C ping | xargs -r kill -SIGQUIT
Run Code Online (Sandbox Code Playgroud)

作为后台工作

while sleep 20; do ps -o pid= -C ping | xargs -r kill -SIGQUIT; done
Run Code Online (Sandbox Code Playgroud)

FreeBSD(在 pfSense 2.4.5 上测试,基于 FreeBSD 11.3)

发送INFO信号。

CTRL通过+发送时的示例输出T

64 bytes from 127.0.0.1: icmp_seq=137 ttl=64 time=0.328 ms
64 bytes from 127.0.0.1: icmp_seq=138 ttl=64 time=0.028 ms
load: 0.18  cmd: ping 62483 [select] 144.69r 0.00u 0.01s 0% 2256k
139/139 packets received (100.0%) 0.018 min / 0.072 avg / 0.505 max
64 bytes from 127.0.0.1: icmp_seq=139 ttl=64 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=140 ttl=64 time=0.027 ms
Run Code Online (Sandbox Code Playgroud)

通过以下方式发送时的示例输出kill

64 bytes from 127.0.0.1: icmp_seq=137 ttl=64 time=0.328 ms
64 bytes from 127.0.0.1: icmp_seq=138 ttl=64 time=0.028 ms
139/139 packets received (100.0%) 0.018 min / 0.072 avg / 0.505 max
64 bytes from 127.0.0.1: icmp_seq=139 ttl=64 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=140 ttl=64 time=0.027 ms
Run Code Online (Sandbox Code Playgroud)

运行时发送

CTRL+ T=status根据stty -a.

发送到一个或多个已知的 pid

while sleep 20; do ps -o pid= -C ping | xargs -r kill -SIGQUIT; done &
Run Code Online (Sandbox Code Playgroud)

发送给所有正在运行的人

64 bytes from 127.0.0.1: icmp_seq=137 ttl=64 time=0.328 ms
64 bytes from 127.0.0.1: icmp_seq=138 ttl=64 time=0.028 ms
load: 0.18  cmd: ping 62483 [select] 144.69r 0.00u 0.01s 0% 2256k
139/139 packets received (100.0%) 0.018 min / 0.072 avg / 0.505 max
64 bytes from 127.0.0.1: icmp_seq=139 ttl=64 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=140 ttl=64 time=0.027 ms
Run Code Online (Sandbox Code Playgroud)

定期发送给所有正在运行的

64 bytes from 127.0.0.1: icmp_seq=137 ttl=64 time=0.328 ms
64 bytes from 127.0.0.1: icmp_seq=138 ttl=64 time=0.028 ms
139/139 packets received (100.0%) 0.018 min / 0.072 avg / 0.505 max
64 bytes from 127.0.0.1: icmp_seq=139 ttl=64 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=140 ttl=64 time=0.027 ms
Run Code Online (Sandbox Code Playgroud)

作为后台工作

kill -INFO <pid> [...]
Run Code Online (Sandbox Code Playgroud)

注释和致谢

感谢@pmos 的回答,它首先为我播下了周期性方法的想法,并感谢所有其他对此做出贡献的答案和评论。

感谢@VictorYarema提出将我的评论转化为实际答案的建议。原始评论中的方法的本质是相同的,但随着时间的推移已经演变如下:

  • 添加了-r选项以xargs防止kill在没有结果时在没有 pid 的情况下运行ps
  • 为了摆脱标题行,ps我使用了-o pid=将列标题文本设置为空的选项,而不是h因为h对于 Linux 和 FreeBSD 有不同的含义
    • 虽然Linux 中ps支持显式--no-headers选项,但 FreeBSD 则不然
  • sleep命令移至while测试条件
  • 删除了不必要的引号
  • 移植到 FreeBSD


Ste*_*gin 7

Mac 上它是Ctrl+ T

Ctrl+\确实一样Ctrl+ C,因为它示出了统计之后停止平。


小智 6

试试Ctrl+4

它显示了这样的一行:

312/312 packets, 0% loss, min/avg/ewma/max = 0.312/1.236/0.505/208.655 ms
Run Code Online (Sandbox Code Playgroud)