Monitors port reachability with notifications via Telegram and Gotify
This script monitors the reachability of a specific port on a given IP address. If the port becomes unreachable, it sends notifications about the port's status via Telegram and Gotify, avoiding repeated alerts until the port is reachable again. Upon recovery, a notification is sent, and monitoring continues.
This script monitors the reachability of a specific port on a server and sends notifications via Telegram and Gotify when the port is unreachable or back online.
Before starting, ensure you have:
-
Bash Installed
-
Telegram Setup
- Create a Telegram bot and obtain its API token by following this guide.
- Get your Telegram chat ID using the Telegram bot API.
-
Gotify Setup
- Set up a Gotify server (instructions).
- Obtain a valid Gotify app token.
-
Dependencies
- Ensure the
nc(Netcat) andcurlcommands are installed:sudo apt install netcat curl # For Debian/Ubuntu sudo yum install nc curl # For CentOS/RHEL
- Ensure the
-
Clone or Download the Script
- Clone this repository or download the
port_monitor.shfile.
- Clone this repository or download the
-
Edit the Script Open
port_monitor.shin a text editor and replace the placeholder values with your own:-
IP/Hostname and Port to Monitor:
ipToCheck="your-server-address.com" # Replace with the server address or IP you want to monitor portToCheck=22 # Replace with the port number you want to check
-
Telegram Credentials:
telegramToken="YOUR_TELEGRAM_BOT_TOKEN" # Replace with your Telegram bot API token telegramChatId="YOUR_TELEGRAM_CHAT_ID" # Replace with the Telegram chat ID
-
Gotify Credentials:
gotifyServer="http://your-gotify-server.com" # Replace with your Gotify server URL token="YOUR_GOTIFY_APP_TOKEN" # Replace with your Gotify app token
-
Time Settings (optional):
timeoutSeconds=5 # Time in seconds to wait for a connection intervalMinutes=8 # Time in minutes between checks
-
-
Save the File
-
Make the Script Executable Run the following command to give the script execution permissions:
chmod +x port_monitor.sh
-
Run the Script Execute the script using:
./port_monitor.sh
You can use
screentooscreen -dmS PortWatcher ./port_monitor.sh
-
Press
Ctrl+Cto stop the script. It will terminate cleanly and stop monitoring.
- Down Alert: When the monitored port becomes unreachable, notifications will be sent via Telegram and Gotify.
- Recovery Alert: When the port is back online, a recovery notification will be sent.
-
Error: Command Not Found
- Ensure
nc(Netcat) andcurlare installed:sudo apt install netcat curl # For Debian/Ubuntu sudo yum install nc curl # For CentOS/RHEL
- Ensure
-
Telegram Not Sending Messages
- Double-check the
telegramTokenandtelegramChatIdvalues. - Test your bot by visiting this URL in your browser:
https://api.telegram.org/bot<YOUR_TELEGRAM_BOT_TOKEN>/sendMessage?chat_id=<YOUR_CHAT_ID>&text=TestMessage
- Double-check the
-
Gotify Not Sending Messages
- Ensure your Gotify server is running and accessible.
- Verify your app token and server URL.
-
Script Not Running
- Ensure the script has executable permissions:
chmod +x port_monitor.sh
- Make sure you're running the script in a Bash-compatible terminal.
- Ensure the script has executable permissions: