Answer the question
In order to leave comments, you need to log in
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
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 &
killall ping
127.0.0.X
with their addresses? 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
while fping -q 8.8.8.8 ; do echo "Ok";sleep 2; done && echo "Связь пропала" && fping -l IP1 IP2 IP3 IP4 &> ip.log.txt
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 questionAsk a Question
731 491 924 answers to any question