Here's a real result of my ping 192.168.1.1
command:
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
For some unknown reasons, sometimes my WiFi connection gets very slow and the main cause is the ping time.
I should manually disconnect my WiFi and reconnect it.
I'm on Ubuntu 20.04 LTS.
I want to make it automatic. Here are the script snippets I came up with. But I can't put them together:
# 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
I tried this, but it does not work:
while read Line; do
echo "read line"
echo $Line
done <<< $(ping 192.168.1.1)
Basically, I'm stuck at redirecting ping
standard output to a while loop, and I'm stuck at aggregating the average time using the awk
command.
How can I combine them together?
Update
For anyone interested in the final script, please see my answer below.