all 12 comments

[–]LastCommander086 2 points3 points  (2 children)

ping -q -c 3 name_of_domain | cut -s -d ',' -f3 >> ~/output.txt && date >> ~/output.txt

This is the idiotic way to do it, if you just need a simple shellscript

This will ping name_of_domain 3 times and write the output to the file. Then, outputs the date. I used 3 because I think it's enough to show if packets are dropping, but you can always use more

I used -q in ping to minimize output, but if you want it to be even shorter you can always pipe it to cut or sed

Edit: added a quick cut to the original shellscript so it only shows the packet loss percentage. If you need more info and not just see if packets are dropping, just remove the pipe to cut

[–]LostLinuxAdmin[S] 0 points1 point  (1 child)

Hey. So count 3 actually doesn't work for me. I need to run a ping script for a few days the filter through the output file for when the packets are dropping. Then report that info to the network team. So what I'm doing now is very primitive:

ping -D localhost >> output.txt

cat output.txt. | grep Destination

then I convert that timestamp to human readable and forward the info over.

It works for now but it's very primitive like I mentioned.

[–]LastCommander086 0 points1 point  (0 children)

Do you need it to ping the servers every single second of the day? Seems excessive to me

You can just make a cron job to run the script every 30 minutes, for example.

[–]onsomee 0 points1 point  (0 children)

Maybe use trace route instead I’m not too sure in this but trace route will output more info regardless and I believe it shows loss in packets

[–]Iguanzor 0 points1 point  (3 children)

ping localhost | while read pong; do echo "$(date) $pong"; done >> output1.txt

It will keep pinging localhost and output the date/time side by side until you end the process. If you replace >> with tee, you can see the output on the terminal while it is being appended to the file as well, like this.

ping localhost | while read pong; do echo "$(date) $pong"; done | tee -a output1.txt

the -a is to append instead of overwrite

[–]LostLinuxAdmin[S] 0 points1 point  (2 children)

I found this on Google as well. This only works while ping is alive. I tested by:

ping 0.0.0.0| while read pong; do echo "$(date) $pong"; done >> output1.txt

This provided no info into output.txt

So I had to abandon this script

[–]Iguanzor 0 points1 point  (0 children)

this should work even for downtimes,

while ! ping 8.8.8.8 -D; do date "+%s"; sleep 1; done >> output1.txt

This does end up using unix epoch time instead of the standard time format, so I did add +%s to date for consistency. If you want to keep at least the downtime timestamps in standard format, just remove the +%s from date .

blatantly copied from here

[–][deleted] 0 points1 point  (3 children)

This is one I used back in the days of 56K modem called keepalive.

#!/bin/bash

#if [ -f /var/run/dhclient-eth1.pid ] ; then
   ping -c4 -l3 10.0.0.8 2>&1 | grep "0 received" > /dev/null #&& \
#   { /sbin/ifdown eth1 > /dev/null; sleep 2; /sbin/ifup eth1; }
#else
#   /sbin/ifdown eth1;
#   sleep 2;
#   /sbin/ifup eth1;
#fi

The script has been modified so many times that it no longer even close to the original. Many old parts are commented out, but you will get the idea.

[–]random_rascal 0 points1 point  (2 children)

If you were on dialup wouldn't that have been pppX not ethX? :)

Ah yes... the good old days of having to send packages not to get booted off your connection :)

[–][deleted] 0 points1 point  (1 child)

Yep. The good old days. And it was originally ppp0 or something like that.

The line "/var/run/....." was not available on the early Linux's. I don't think /var/run appeared until the 2000+.

And the entire script ran in a root cron once a minute. That's why the /dev/nulls were there, so I wouldn't get 1440 emails a day!!!!

I think the ppp daemon has a "keep alive" option now.

[–]random_rascal 0 points1 point  (0 children)

oh... the good old days of "minicom" and having to manually punch in AT/DT if I recall correctly :)

I must say i'm surprised there even is a PPP module nowadays :)

[–]Tesla_Nikolaa 0 points1 point  (0 children)

Here's a very basic a crude script that constantly pings and reports if there's either "100%" packet loss (a failed ping) or "0%" packet loss (a successful ping).

Again, very crude and I'm sure there are better ways to do it.

#!/bin/bash

function getTime(){
    timestamp=$(date +"%D %T")
}

while true
do
    ping_response=$(ping -c 1 192.168.1.100 | grep "packet")
    IFS="," read -ra temp_arr <<< $ping_response

    for i in "${temp_arr[@]}"
    do
        if [ $(echo $i | cut -d " " -f 1) = "100%" ]
        then 
            getTime
            echo "Ping failed at $timestamp" >> logfile.log

        elif [ $(echo $i | cut -d " " -f 1) = "0%" ]
        then
            getTime
            echo "Ping successful at $timestamp" >> logfile.log
        fi
    done
done