如何获取平均 ping 时间?

如何获取平均 ping 时间?

ping 192.168.1.1这是我的命令的实际结果:

64 bytes from 192.168.1.1: icmp_seq=964 ttl=64 time=1018 ms
64 bytes from 192.168.1.1: icmp_seq=965 ttl=64 time=921 ms
64 bytes from 192.168.1.1: icmp_seq=966 ttl=64 time=847 ms
64 bytes from 192.168.1.1: icmp_seq=967 ttl=64 time=866 ms
64 bytes from 192.168.1.1: icmp_seq=968 ttl=64 time=895 ms
64 bytes from 192.168.1.1: icmp_seq=969 ttl=64 time=858 ms
64 bytes from 192.168.1.1: icmp_seq=970 ttl=64 time=886 ms
64 bytes from 192.168.1.1: icmp_seq=971 ttl=64 time=890 ms
64 bytes from 192.168.1.1: icmp_seq=972 ttl=64 time=888 ms
64 bytes from 192.168.1.1: icmp_seq=973 ttl=64 time=910 ms
64 bytes from 192.168.1.1: icmp_seq=974 ttl=64 time=915 ms
64 bytes from 192.168.1.1: icmp_seq=975 ttl=64 time=937 ms
64 bytes from 192.168.1.1: icmp_seq=976 ttl=64 time=933 ms
64 bytes from 192.168.1.1: icmp_seq=977 ttl=64 time=947 ms
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
64 bytes from 192.168.1.1: icmp_seq=985 ttl=64 time=1.09 ms
64 bytes from 192.168.1.1: icmp_seq=986 ttl=64 time=2.02 ms
64 bytes from 192.168.1.1: icmp_seq=987 ttl=64 time=3.37 ms
64 bytes from 192.168.1.1: icmp_seq=988 ttl=64 time=1.08 ms
64 bytes from 192.168.1.1: icmp_seq=989 ttl=64 time=2.87 ms
64 bytes from 192.168.1.1: icmp_seq=990 ttl=64 time=1.11 ms
64 bytes from 192.168.1.1: icmp_seq=991 ttl=64 time=1.39 ms
64 bytes from 192.168.1.1: icmp_seq=992 ttl=64 time=1.11 ms
64 bytes from 192.168.1.1: icmp_seq=993 ttl=64 time=1.10 ms

由于某些未知的原因,有时我的 WiFi 连接会变得非常慢,主要原因是 ping 时间。

我应该手动断开我的 WiFi 并重新连接。

我在使用 Ubuntu 20.04 LTS。

我想让它自动化。以下是我提出的脚本片段。但我无法将它们放在一起:

# Read time using awk
ping 192.168.1.1 | awk '{gsub("time=", ""); print $7}'

# Disconnecting from WiFi
nmcli con down WiFiName

# Reconnecting to WiFi
nmcli device wifi connect

我尝试了这个,但是没有用:

while read Line; do
    echo "read line"
    echo $Line
done <<< $(ping 192.168.1.1)

基本上,我被困在将ping标准输出重定向到 while 循环,并且我被困在使用awk命令聚合平均时间。

我怎样才能将它们结合在一起?

更新

对于那些对最终脚本感兴趣的人,请参阅下面的回答。

答案1

管道或while循环正在等待输入,但您的ping命令永远不会结束。

您可以使用-c-w标记:

-c <count>         stop after <count> replies
-w <deadline>      reply wait <deadline> in seconds

例如

ping -c10 192.168.1.1  | awk '{gsub("time=", ""); print $7}'

但为了获得平均时间,您可以更轻松地解析摘要:

rtt min/avg/max/mdev = 0.475/0.475/0.475/0.000 ms
ping -q -c10 192.168.1.1 | awk -F'[/=]' 'END{print $2, $6}'
avg 0.475

答案2

以下是我最终想出的脚本:

echo
echo 'What is the WiFi name?'
read WiFiName

if [ $WiFiName == '' ]; then
    echo "WiFi name can not be empty"
    exit;
fi

echo "Now checking ..."
echo

while true; do
    AverageTime=$(ping -q -c5 192.168.1.1 | awk -F'[/=]' 'END{print $6}')
    IsSlow=$(echo "scale=2; ($AverageTime > 5)" | bc)
    if [ $IsSlow == "1" ]; then
        echo "WiFi got slow. The average ping time is $AverageTime. Reconnecting ..."
        nmcli con down $WiFiName
        nmcli device wifi connect $WiFiName
        echo "Reconnected to WiFi."
    else
        echo "Average ping time $AverageTime"
    fi
done

相关内容