Testing ports should be fairly trivial (though probably requires a call to an external service to verify), but the best way of manipulating the webui will vary wildly depending on which variety of router you have and how it's web interface (particularly authentication and potential reliance on javascript) works.
Your best place to begin is probably your browsers debug/trace console and a general web search for "router reboot script" or the like, which yields many examples you might use as a staring point, done with everything from bash and curl to php to python and selenium/webdriver.
For full headless-browser based automation, in the past I might have suggested phantomjs... but that project appears dead, and I suspect everyone is using
selenium these days. I gather there are even firefox extensions to record interactions for later playback.
If you're remotely handy with go, there's also
surf, which would be a rather lighter solution while still behaving much like a real browser.
OTOH, if your router supports management over SSH or telnet I'd be inclined to take that as the much easier option.
Once is happenstance. Twice is coincidence. Three times is enemy action. Four times is Official GNOME Policy.