Created
February 10, 2024 15:32
-
-
Save luxzg/7fef55ca95683ce9aed7e1a1fd033a01 to your computer and use it in GitHub Desktop.
Ping check script (Bash / Telegram)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# script by LuxZg 2024-02-10 ; simple ping idea by Gordon (see below) | |
# call this script in bash like this (you can use some other IP): | |
# ./ping_test_01.sh 8.8.8.8 | |
# Telegram information - MAKE SURE YOU HAVE YOUR OWN TELEGRAM BOT SET UP ALREADY! | |
# you only need to change TOKEN and ID, nothing else needs to be changed in this script! | |
# optionally change the time zone (in 3 places, scroll down) and path to the .log file (by the end) | |
TOKEN="putyour:bottoken-inhere" | |
ID="yourchatID" | |
URL="https://api.telegram.org/bot$TOKEN/sendMessage" | |
# initialize the MESSAGE so we're starting blank, but so we know it was reset | |
MESSAGE="Start session - " | |
# simple ping check thanks to StackOverflow answer: | |
# Gordon Davisson, Dec 16, 2022 at 22:35 | |
# https://stackoverflow.com/a/74830395/13312932 | |
# If the target is initially up, we need to print that | |
# before going into the monitor loop. | |
if ping -c 1 $1 &> /dev/null | |
then | |
# instead echo, we will put the message in the MESSAGE variable | |
MESSAGE+="Target is up at: $(TZ='Europe/Zagreb' date) - " | |
# for testing we can echo the MESSAGE | |
# echo ${MESSAGE} | |
fi | |
while : | |
do | |
# At this point in the script, the target is either | |
# up and we need to wait for it to go down, or we | |
# just started and it's down (in which case this | |
# loop will exit immediately). | |
while ping -c 1 $1 &> /dev/null | |
do | |
sleep 5 | |
done | |
# At this point, it's gone down; print a note, and | |
# wait for it to come back up. | |
# again, instead echo, concatenate the new info to MESSAGE | |
MESSAGE+="Target is down at: $(TZ='Europe/Zagreb' date) - " | |
# and we can test with echo | |
# echo ${MESSAGE} | |
until ping -c 1 $1 &> /dev/null | |
do | |
sleep 5 | |
done | |
# It's back up! Print another note, then go back to | |
# waiting for it to fail. | |
# one last time, concatenate to MESSAGE | |
MESSAGE+="Target is up at: $(TZ='Europe/Zagreb' date) - " | |
# and we can test with echoing the MESSAGE | |
# echo ${MESSAGE} | |
# if device is up and link is up, means we can send the MESSAGE to Telegram bot | |
# and log the bot response to local .log file | |
curl -s -X POST $URL -d chat_id=$ID -d text="${MESSAGE}" >> /home/localadmin/ping_test_01.log | |
# we reset the MESSAGE for the next loop | |
MESSAGE="Start new session - " | |
# echo ${MESSAGE} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to monitor your own Internet connection from inside in a logical way
Luka Pribanić Lux (LuxZg) - 2024-Feb-10
Issues: when there's no connection, you can't post alerts, and you want some kind of logs
GitHub Gist made based on this how-to: https://gist.github.com/luxzg/7fef55ca95683ce9aed7e1a1fd033a01
Note: Make your Telegram bot before this, and make sure you have Linux box with Internet connection working
Login to Linux box and create script; copy paste contents below, making sure to change bot token, channel ID, time zone, log path
nano /home/localadmin/ping_test_01.sh
<copy script from this Gist into this script>
Make script executable
chmod +x /home/localadmin/ping_test_01.sh
ll /home/localadmin/ping_test_01.sh
Then run it; fake the disconnect, then 10 sec later connect back; you can ping 8.8.8.8 or something else
./home/localadmin/ping_test_01.sh 8.8.8.8
Check the log file
cat /home/localadmin/ping_test_01.log
If you are not satisfied, you can delete "#" in front of "echo" commands in the .sh script, and try test again,
You will see messages in the console, it will make debugging easier
Once script is working when ran manually, time to make it a service
I used this tutorial: https://linuxconfig.org/how-to-run-script-on-startup-on-ubuntu-22-04-jammy-jellyfish-server-desktop
Go to service directory and make a new file
Copy paste this, make sure the path to script is full and correct path, and if you want change 8.8.8.8 target to whatever you want
Change permissions
Reload the list of services, enable, and start the service (so you don't need to reboot to test), check status
Status should be like :
These two are just for debugging, if you want to stop or disable the service later
Either wait for real downtime, or fake it again, so you can confirm service is working, you can check the log
cat /home/localadmin/ping_test_01.log
Service will keep working even if you logoff from SSH
Telegram sample messages: