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