K
K
karpo5182020-05-02 17:48:49
linux
karpo518, 2020-05-02 17:48:49

How to ping using a bash script when the Internet connection is lost?

A home PC running Linux Mint has regular short-term failures in its fiber optic internet connection. To diagnose the TP, the provider asked to ping 4 ip addresses at the time of the problem. As I said, internet problems are temporary and I can't monitor them manually. I would like to run a bash script on the computer that will monitor information about the status of the Internet connection and, in case of a problem, will ping the necessary ip addresses. ping should run from the moment the problem occurs until the connection is re-established + 1 minute. The result is in the form of data on transmitted and lost packets, i.e. the entire log of ping commands must be written to 4 text files. I suppose that there is nothing complicated here, but I do not have enough experience in writing such scripts. Can you share an example?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AUser0, 2020-05-02
@AUser0

IMHO it's easier to just run four pings with errors saved to files, and after a break in the Internet, kill the still pinging processes, feed the files to tech support.
Run like this for example:

ping -i5 -n 127.0.0.1 | grep -v ' time=' >ping1.log 2>&1 &
ping -i5 -n 127.0.0.2 | grep -v ' time=' >ping2.log 2>&1 &
ping -i5 -n 127.0.0.3 | grep -v ' time=' >ping3.log 2>&1 &
ping -i5 -n 127.0.0.4 | grep -v ' time=' >ping4.log 2>&1 &

And to kill: can you guess to replace
killall ping
IP-shniks 127.0.0.Xwith their addresses?
PS Normally departed-returned requests will NOT be included in the log file . But the file will not swell to incredible sizes.

X
xotkot, 2020-05-02
@xotkot

for scripts and mas ping, it is easier to use the fping
fping -l IP1 IP2 IP3 IP4
utility to redirect output to a file along with errors: there is also a mtr
fping -l IP1 IP2 IP3 IP4 &> ip.log.txt
utility for visual monitoring
mtr 8.8.8.8

echo -e "IP1\nIP2\nIP3\nIP4" | mtr -rw -F - > mtr-report

cut off by Ctrl + c or add a key to the command that regulates the number of requests, for example -c5
one-liner example:
while fping -q 8.8.8.8 ; do echo "Ok";sleep 2; done && echo "Связь пропала" && fping -l IP1 IP2 IP3 IP4 &> ip.log.txt

run in the terminal when the network is working normally and as soon as the ping to the Google dns 8.8.8.8 disappears (you can use a different address), then fping will automatically start at four addresses, as the statistics are collected, we cut off the command manually Ctrl + c or kill this process in another terminal, that's it the data will be saved in the ip.log.txt file, which will be located next to the script

K
Karpion, 2020-05-03
@Karpion

I think it's easiest to just run pings at system startup, piping their output to a parser (see below). and the output of the parser is to a file.
The parser is easiest to write in AWK, Perl, Python - whichever you know best. The program reads a stream of data from stdin into a buffer; while everything is fine, the buffer is simply rotated (old row entries are discarded, new ones are placed in their place; the buffer is needed for about five lines); when a crash occurs, the buffer is written to stdout and crash messages are immediately written there; when fixing a crash, some more messages are written to stdout. Well, stdout is redirected to a file.
Or it’s completely stupid: write the ping output to a file, and then cut the necessary pieces from the file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question