Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 358  / 3 Years ago, thu, september 2, 2021, 6:08:44

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.


More From » networking

 Answers
1

The pipe or while-loop are waiting for input, but your ping command never ends.


You can use -c or -w flag:


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

e.g.


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



But to get the average time, you can more easily parse the summary:


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

[#495] Friday, September 3, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uffno

Total Points: 283
Total Questions: 93
Total Answers: 111

Location: Saint Vincent and the Grenadines
Member since Thu, Oct 15, 2020
4 Years ago
uffno questions
Sun, Apr 2, 23, 15:15, 1 Year ago
Sat, Jan 8, 22, 16:37, 2 Years ago
Fri, Feb 10, 23, 02:11, 1 Year ago
;