A solution for my port 80 block just by rebooting the Zyxel Gateway and making auto reboot when my website is unreachable

Rant

When you like me having own Web Server running on a bad Zyxel Gateway that keeps blocking on port 80 because of too much incomming connection?
Not sure if my Internet Provider (Telfort) are at fault, I remember when I use Speedtouch I never had that problem before. I believe my Zyxel is a bad product and my Internet Provider might want to limit me. Or someone doing random attacks on my router.
I think it is weird when a Gateway blocks the port for no reason. The funny thing is I can access my server LAN trough port 80, except from WAN. And I can use SSH to my Web Server through WAN. So it may have something to do with my Internet Provider and Zyxel.

Idea

So a temporary fix for this issue

  • configure my Zyxel to open SSH for Zyxel locally
  • use crontab on my web server to check if domain is reachable from the internet, and check every 20 minutes.
  • email myself when my domain is unreachable
  • reboot Zyxel from web server only if my domain is unreachable AND the connection between Zyxel and the Web Server are ok.

Tools

The stuff I use

  • Dynamic DNS: no-ip.com
  • Zyxel Remote MGMT
  • expect
  • crontab
  • zyxel.sh
  • reboot_zyxel.exp
  • sendMailUptimeReport.sh

Reboot Zyxel from SSH

First you need to login your Zyxel on your browser and head to Remote MGMT to turn on SSH LAN.

On my Zyxel P-2812HNU-F1 mainpage
Maintenance / Remote MGMT /
SSH - LAN: Enable
SSH - Port: 222

Now you can use your terminal to log in

ssh -p 222 admin@192.168.1.254

Use the same username and password when you log from browser to config your Gateway

admin@192.168.1.254's password: 
No entry for terminal type "xterm-256color";
using dumb terminal settings.
ZySH> 

And reboot your Gateway with reboot.

ZySH> reboot

Need to edit the rest of the guide

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# sends myself an email if server is down
STATUSCODE=`curl -I hariantoatwork.zapto.org 2>|/dev/null | awk '/^HTTP/ { print $2}'`
MAILFROM='"Sandbox Ubuntu"<server@oib.mdstn.com>'

function check_uptime {
	if [ "$STATUSCODE" -ne "200" ]; then
		echo "ERROR: Your router is dead" | mail -aFrom:"$MAILFROM" -s 'Uptime Report' uptime@oib.mdstn.com
		$DIR/zyxel.sh
	else
	#	echo "OK: Your server is still on" | mail -aFrom:"$MAILFROM" -s 'Uptime Report' uptime@oib.mdstn.com
		echo "OK: Your server is still on"
	fi
}

check_uptime

the file: sendMailUptimeReport.sh

#ssh -p 222 admin@192.168.1.254
# reboot Zyxel Router
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function restart_zyxel {
	echo "Restart Zyxel"
	ZYXEL_STATUSCODE=`curl -I 192.168.1.254 2>|/dev/null | awk '/^HTTP/ { print $2}'`
	if [ "$ZYXEL_STATUSCODE" == "200" ]; then
		expect $DIR/reboot_zyxel.exp password
		echo "Rebooting Zyxel Router!!!!"
	fi
}

restart_zyxel

the file: zyxel.sh

#!/usr/bin/expect

spawn ssh -p 222 admin@192.168.1.254
set password [lindex $argv 0]

expect "*?assword:*"
send -- "$password\r"
send -- "reboot\r"

the file: reboot_zyxel.exp

# on every 20 minutes check http://mdstn.com if online, when false send mail to uptime@oib.mdstn.com
*/20 * * * * /home/ubuntu/bin/sendMailUptimeReport.sh

the crontab: sudo crontab -e