Skip to content

Instantly share code, notes, and snippets.

@luxzg
Created February 10, 2024 15:32
Show Gist options
  • Save luxzg/7fef55ca95683ce9aed7e1a1fd033a01 to your computer and use it in GitHub Desktop.
Save luxzg/7fef55ca95683ce9aed7e1a1fd033a01 to your computer and use it in GitHub Desktop.
Ping check script (Bash / Telegram)
#!/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
@luxzg
Copy link
Author

luxzg commented Feb 10, 2024

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

cd /etc/systemd/system/
nano /etc/systemd/system/ping_test_01.service

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

[Unit]
After=network.target

[Service]
ExecStart=/home/localadmin/ping_test_01.sh 8.8.8.8

[Install]
WantedBy=default.target

Change permissions

chmod 744 /home/localadmin/ping_test_01.sh
chmod 664 /etc/systemd/system/ping_test_01.service

Reload the list of services, enable, and start the service (so you don't need to reboot to test), check status

systemctl daemon-reload
systemctl enable ping_test_01.service
systemctl start ping_test_01.service
systemctl status ping_test_01.service

Status should be like :

	Active: active (running)
		/bin/bash /home/localadmin/ping_test_01.sh 8.8.8.8
		sleep 5
			OR (if you are very lucky to hit the correct pinging moment)
		ping -c 1 8.8.8.8

These two are just for debugging, if you want to stop or disable the service later

# systemctl stop ping_test_01.service
# systemctl disable ping_test_01.service

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:

Start session - Target is up at: Sat Feb 10 16:42:28 CET 2024 - Target is down at: Sat Feb 10 16:42:49 CET 2024 - Target is up at: Sat Feb 10 16:42:49 CET 2024 -
Start new session - Target is down at: Sat Feb 10 17:24:32 CET 2024 - Target is up at: Sat Feb 10 17:28:49 CET 2024 -
Start new session - Target is down at: Sat Feb 10 20:09:27 CET 2024 - Target is up at: Sat Feb 10 20:09:27 CET 2024 -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment