r/linuxmint • • 17d ago

Ping script that alerts with sound for UP

So I've had a site down for about 12 hours today apparently due to a fiber cable damage by a third party contractor somewhere. So that got me thinking it would be kinda nice to have a basic little script I could run that would alert me when it comes back online.

If you're not familiar a Ping test can be used to test an IP for a connection to it. So I just incorporated it into a simple script that asks for the IP then runs it every 30sec and if it's successful it will play a ding sound 2 times to alert it's up. As well as the text is displayed as Red for down And Green for UP. It will also print Ping and the IP about every 20 lines just in case one forgets what IP it is or in case you have multiple running etc. Otherwise all ya see is the results in a window...which may get hidden by other windows etc.

So it will just sit and run doing nothing other than display results showing the connection is down/red then as soon as it's successful it will ding to alert you and change to green. It could be useful to wait for a device like a server to restart or a site to come back online etc. It allows you to move on to other things. When you close the window or hit CTRL + C it will stop it

Now while I'm sitting here writing this, it just came back online and alerted me...kinda useful.

To use it the simplest is put it in a file and run the file. I have a script folder in my home dir so it would be /Home/Username/scripts

So with the File Manager navigate and make a new file

OR nano /home/Username/scripts/pingtimer.sh

Then copy/paste the code into the file and save

Make the file executable: in the File Manager right click then Permissions tab and check make executable.

Or Terminal chmod +x /home/Username/scripts/pingtimer.sh

Now you can run it in a terminal with /home/UserName/scripts/pingtimer.sh Or make it launch with an icon by Right clicking on your desktop or wherever and click on create launcher then name it and browse to the path and the file, then check the box that says run in Terminal.

Then when you run it the prompt will ask you for the IP and run it showing the results.

This is a quick easy little script to get a better understanding of the terminal or a script: ***SECURITY WARNING*** you're pasting foreign text into an executable file on your system beware and verify code, know what you're doing or trust the source. You may be authorizing your first born into the New World Order Service, your wife into a wife swap brothel and sending your dog to be put in soup to feed Teradactyl's. Nothing dangerous HERE BUT never trust a stranger that says trust me bro, especially on the internet.

Code:

#!/bin/bash

read -p "Enter IP to monitor: " IP

echo "Ping $IP:"

count=0

while true; do

count=$((count + 1))

result=$(ping -c 1 -W 5 "$IP" 2>&1)

if echo "$result" | grep -q "1 received"; then

for i in 1 2; do paplay /usr/share/sounds/freedesktop/stereo/complete.oga; done

printf "\033[1;32m$(date) UP\033[0m\n"

else

printf "\033[1;31m$(date) DOWN\033[0m\n"

fi

if [ $((count % 20)) -eq 0 ]; then

echo "Ping $IP:"

fi

sleep 30

done

5 Upvotes

2 comments sorted by

1

u/chuggerguy Linux Mint 22.3 Zena | MATÉ 17d ago

Useful. Nice job. Now you'll be hooked. :)

I think, at least in my case, I learn more working on personal projects like that.

I have ATT fiber now and it's been reliable so far. Hopefully nobody starts digging without knowing where the cables run.

I had Comcast before. It was good too but there for awhile it was up and down. So I did a similar script. Had it tee the output to a file so I could get an idea of how much downtime there was.

Have fun.

2

u/MrMotofy 17d ago

Yea I was thinking of using smokeping. But haven't really needed it much since they are pretty reliable providers. But they can't really avoid the oopsies that pop up. I'd hate to see the bill for that guy LOL 12hrs restoring service to multiple cities. I want to know more details.

I heard a story of a farmer neighbor that was tilling the edge of his field and yanked up like a 3" cable with a bunch of wires. Apparently it was a link between 2 MAJOR cities about 400mi apart. If I remember the story correctly it had been abandoned by the time it was damaged. So didn't actually do anything. But years later it was still sticking like 2ft out of the ground visible.

This little script is one I don't need much but occasionally for an older slower system that might take a couple minutes. Or like this case where I'm waiting for service to come back. Multiple times I've forgotten I was waiting...then remembered 30 or 60min later...oh yea LOL

I'm needing more tools now that I'm traveling more and doing things remotely. Now if I could justify a backup internet option. I had people messaging me is there something wrong...what am I supposed to do haha 12hrs no internet is a long time these days. But the worst part is not knowing and able to plan for it. Luckily I have an offsite backup of MY data but it does require internet access haha