Get Email Notification When a Server or Port is Unreachable

This simple script is useful if you want to monitor a certain host/ip/port and receive email notification if is unreachable.
I know, someone more experienced would find a simple way to do it but this is my version.

A1 - FOR A HOST OR IP (modify green lines)

cat >> /jffs/scripts/server-ping << 'EOF'
#!/bin/sh
#######Email credentials
FROM_ADDRESS="[email protected]"
TO_NAME="your-name"
TO_ADDRESS="[email protected]"
USERNAME="username"
PASSWORD="password"
SMTP="smtp.email.com"
PORT="465"
SMTP_ENCRYPT="smtps" # Set encryption type: SSL = smtps (default), TLS = smtp
FROM_NAME="$(nvram get productid) Router"
PINGHOST="192.168.1.100" #or "myhost.com"
#######Script
logger -s "$0" "Pinging host $PINGHOST..."
ping -c 3 $PINGHOST;
if [ $? -eq 0 ]; then
echo "Server is reacheable :)";
else
echo "Server is down or the provided host is wrong!"
#######Email body
echo MIME-Version: 1.0 >/tmp/hmail.txt
echo Content-Type: text/html >>/tmp/hmail.txt
echo "From: \"$FROM_NAME $ROUTER_MODEL\" <$FROM_ADDRESS>" >>/tmp/hmail.txt
echo "To: \"$TO_NAME\" <$TO_ADDRESS>" >>/tmp/hmail.txt
echo "Subject: Server unreachable!" >>/tmp/hmail.txt
echo "Date: $(date -R)" >>/tmp/hmail.txt
echo "" >>/tmp/hmail.txt
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">" >>/tmp/hmail.txt
echo "<html>" >>/tmp/hmail.txt
echo "<head><title></title>" >>/tmp/hmail.txt
echo "</head>" >>/tmp/hmail.txt
echo "<p><b>Server is down or the provided host or ip is wrong!</b></p>" >>/tmp/hmail.txt
echo "<p>`date +%d/%m/%Y` `date +%T`</p>" >>/tmp/hmail.txt
echo "" >>/tmp/hmail.txt
echo "<p><i>Your friendly router.</i></p>" >>/tmp/hmail.txt
echo "" >>/tmp/hmail.txt
echo "</body>" >>/tmp/hmail.txt
echo "</html>" >>/tmp/hmail.txt
#######Email script
curl --url $SMTP_ENCRYPT://$SMTP:$PORT \
--mail-from "$FROM_ADDRESS" --mail-rcpt "$TO_ADDRESS" \
--upload-file /tmp/hmail.txt \
--ssl-reqd \
--user "$USERNAME:$PASSWORD" --insecure -v
rm /tmp/hmail.txt
fi
EOF

Press ENTER
A2 - Modify email credentials

nano /jffs/scripts/server-ping

Save with CTRL-O, ENTER, exit with CTRL-X
A3 - Set permission

chmod +x /jffs/scripts/server-ping

A4 - Create a cron job to run every 10 minutes for example, for another interval use this cron generator

nano /jffs/scripts/init-start

Copy/Paste the following lines (remove #!/bin/sh line if already there)

#!/bin/sh
cat >> /var/spool/cron/crontabs/admin << 'EOF'
*/10 * * * * /jffs/scripts/server-ping #server-ping#
EOF

Save with CTRL-O, confirm with ENTER, exit with CTRL-X
A5
- Verify the script

sh /jffs/scripts/server-ping

A6 - Reboot router

reboot

A7 - Verify if cron job is created

cru l
*/10 * * * * /jffs/scripts/server-ping #server-ping#

If you follow all steps exactly, you should receive email notification when desired host/ip is unavailable


B1 - FOR A PORT (modify green lines)

cat >> /jffs/scripts/port-ping << 'EOF'
#!/bin/sh
#######Email credentials
FROM_ADDRESS="[email protected]"
TO_NAME="your-name"
TO_ADDRESS="[email protected]"
USERNAME="username"
PASSWORD="password"
SMTP="smtp.email.com"
PORT="465"
SMTP_ENCRYPT="smtps" # Set encryption type: SSL = smtps (default), TLS = smtp
FROM_NAME="$(nvram get productid) Router"
PINGHOST="192.168.1.100" #or "myhost.com"
PINGPORT="22"
#######Script
logger -s "$0" "Pinging port $PINGPORT..."
echo "QUIT" | nc -w1 $PINGHOST:$PINGPORT
if [ $? -eq 0 ]; then
secs=$((1 * 1)) ; while [ $secs -gt 0 ]; do echo "" ;: $((secs--)) ;done
echo "Port is reacheable :)";
else
echo "Server is down or the provided host/ip:port is wrong!"
#######Email body
echo MIME-Version: 1.0 >/tmp/pmail.txt
echo Content-Type: text/html >>/tmp/pmail.txt
echo "From: \"$FROM_NAME $ROUTER_MODEL\" <$FROM_ADDRESS>" >>/tmp/pmail.txt
echo "To: \"$TO_NAME\" <$TO_ADDRESS>" >>/tmp/pmail.txt
echo "Subject: Server unreachable!" >>/tmp/pmail.txt
echo "Date: $(date -R)" >>/tmp/pmail.txt
echo "" >>/tmp/pmail.txt
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">" >>/tmp/pmail.txt
echo "<html>" >>/tmp/pmail.txt
echo "<head><title></title>" >>/tmp/pmail.txt
echo "</head>" >>/tmp/pmail.txt
echo "<p><b>WARNING!</b></p>" >>/tmp/pmail.txt
echo "<p><b>Pinging $PINGHOST:$PINGPORT was unsuccessful!</b></p>" >>/tmp/pmail.txt
echo "<p><b>Server is down or the provided host/ip:port is wrong!</b></p>" >>/tmp/pmail.txt
echo "<p>`date +%d/%m/%Y` `date +%T`</p>" >>/tmp/pmail.txt
echo "" >>/tmp/pmail.txt
echo "<p><i>Your friendly router.</i></p>" >>/tmp/pmail.txt
echo "" >>/tmp/pmail.txt
echo "</body>" >>/tmp/pmail.txt
echo "</html>" >>/tmp/pmail.txt
#######Email script
curl --url $SMTP_ENCRYPT://$SMTP:$PORT \
--mail-from "$FROM_ADDRESS" --mail-rcpt "$TO_ADDRESS" \
--upload-file /tmp/pmail.txt \
--ssl-reqd \
--user "$USERNAME:$PASSWORD" --insecure -v
rm /tmp/pmail.txt
fi
EOF

Press ENTER
B2 - Modify email credentials

nano /jffs/scripts/port-ping

Save with CTRL-O, ENTER, exit with CTRL-X
B3 - Set permission

chmod +x /jffs/scripts/port-ping

B4 - Create a cron job to run every 10 minutes for example, for another interval use this cron generator

nano /jffs/scripts/init-start

Copy/Paste the following lines (remove #!/bin/sh line if already there)

#!/bin/sh
cat >> /var/spool/cron/crontabs/admin << 'EOF'
*/10 * * * * /jffs/scripts/port-ping #port-ping#
EOF

Save with CTRL-O, confirm with ENTER, exit with CTRL-X
B5
- Verify the script

sh /jffs/scripts/port-ping

B6 - Reboot router

reboot

B7 - Verify if cron job is created

cru l
*/10 * * * * /jffs/scripts/port-ping #port-ping#

If you follow all steps exactly, you should receive email notification when desired port is unavailable.
If you use gmail as address and you have 2-Step Verification, create an app password for this script


Thanks @thelonelycoder for curl email script
This tutorial was requested and is dedicated @Molin Matteo

SUBSCRIBE

To be notified by email when a new tutorial is posted :)

Thanks for Subscribing! I'll keeping you up-to-date with latest tutorials!

Something went wrong, try again...