FreeBSD script/software to check if modem has internet connection and reboot if needed

Notice: Page may contain affiliate links for which we may earn a small commission through services like Amazon Affiliates or Skimlinks.

Solkku

Member
Jul 31, 2015
88
10
8
43
Hi! I was wondering if there was a way in FreeBSD (I have NAS4Free running on my NAS) to check if my modem has internet connectivity and reboot it automatically if it doesn't?

A few weeks ago I was on vacation and suddenly my internet died at home, so I couldn't access my Plex Media Server for the rest of the week. Going on another vacation next week and don't want this to happen again. ;)

It's kind of random how the internet just stops working (probably happened 2-3 times this year), so I'd like to have some script/software to check for internet connectivity, say every 10-15min and reboot the modem if needed. I always shut down my Windows PC when I go out, so this checker would have to run on the NAS.

The problem is that I can't reboot my Thomson TG789vn modem with a direct URL command from the web browser in Windows. I can only get to the "Yes/No"-page with a direct URL.

If I paste in the URL that actually makes the reboot command I get:

(http://192.168.1.1/rebooting.lp?tid=RESTART)

If I paste in the URL to the "Yes/no"-page it loads up just fine, but from here you'd have to manually click the "Yes" button in order to send the reboot command to the modem:

(http://192.168.1.1/restart.lp?tid=RESTART)

If I click "yes" the URL in the browser changes to the one that gives the "Access Error: 500 -- Internal Server Error" error detailed above.

So now the question is: Is it possible to to have such a script/software running if you can't reboot the modem with a direct URL/command or a workaround I haven't thought of? Any tips and tricks would be helpful.
 

EffrafaxOfWug

Radioactive Member
Feb 12, 2015
1,394
511
113
You're probably best off investigating whether your router can be bounced via telnet or SSH - be much simpler than dealing with the web interface.

A quick'n'dirty /bin/sh wrapper function I've used for kit that only has a telnet interface:
Code:
( echo open ${host}
sleep 3
echo ${user}
sleep 3
echo ${pass}
sleep 3
echo ${cmd1}
sleep 3
) | telnet
Usual caveats of "telnet is about as secure as a wet paper bag in a Venusian sandstorm" apply.

However, I would say the ultimate answer would be to get yourself a router that doesn't suck quite so badly :) From your name you're probably Finnish and there are a hojillion good ADSLv2 modems/routers available to you...
 

TechIsCool

Active Member
Feb 8, 2012
263
117
43
Clinton, WA
techiscool.com
You want super simple while you're gone. A plug in wall timer. Have it reset itself once or twice a day.

But I do agree with @EffrafaxOfWug that it really should no be causing the issue. My Uptime on my Modem is well over 3 years right now. The reason for it not being longer is I mandatory reboot for firmware.
 

Solkku

Member
Jul 31, 2015
88
10
8
43
Thank you for your responses!

@TechIsCool A plugin wall-timer would indeed be one of the cheapest solutions, albeit not the most elegant one. :)

@EffrafaxOfWug Indeed I'm Finnish. I don't know if many of the modems are compatible with a 100M fibre optic connection. Would it have to be a VDSL2 modem then? My ISP only offer shitty models it seems... I do live in an old apartment building, so the internet connection comes up to my apartment via the old copper telephone cable. So the internet comes from that 3-pin telephone plug in the wall and not RJ-45 directly.

Also, how is telnet insecure if it only talks internally within the LAN network between the modem and the NAS box?
 

RTM

Well-Known Member
Jan 26, 2014
956
359
63
Judging from the data I can find on that model, it is a VDSL2 router, so if you want to replace it, you must get something that supports VDSL2.
I would suggest looking into whether others using the same provider have been able to replace it, before spending any money on something that has no guarantee to even work.

I have not used Nas4Free, but if you are "lucky", the request the browser sends to the router to reboot it does not require that you have previously authenticated. If that is the case you could make a simple payload file with the request, and send it to the server with something like nc/netcat/ncat/etc.

The request file (payload_file) could look like this (very simple example):
GET /reboot HTTP/1.1
Host: 192.168.1.1

Then you would send it doing something like this:
nc 192.168.1.1 80 < payload_file

To figure out what requests your browser ends up sending when rebooting, you can use a tool like Firebug or perhaps developer tools in your Firefox/Chrome Browser (I have no idea what IE or Safari has available, but it's possible they have a developer tools toolbar) or even just analyzing the requests in Wireshark.
 

EffrafaxOfWug

Radioactive Member
Feb 12, 2015
1,394
511
113
My mistake - thought the Thomson TG789vn was an ADSL2 router, not a fibre one. But then you make reference to a 100Mb fibre connection that comes in over copper phone lines...? Is this perhaps an FTTC using VDSL over the last hop?

Reading the TG789vn manual gives bugger-all information about what stuff it supports sadly enough...! Essentially what gauges "compatibility" is what network termination equipment your ISP saddled you with; from the sounds of it you're dealing with whatever the Finnish equivalent of the RJ11 is (this thing?) - can't say for sure but I suspect your connection is ADSL2 or VDSL2 - and VDSL2 would be the only thing capable of carrying a 100Mb/s signal I think. But can't speak authoritatively since I'm unfamiliar with Finnish internet packages... other than they have a reputation for being better and cheaper than ones in the UK :)

telnet's insecure simple by dint of not being encrypted - as you point out it's only a risk if someone can sniff your network but it means you're still punting the credentials out to your network in the clear. Always something to be wary of especially if you have wireless ethernet on an unsegmented VLAN.
 

Solkku

Member
Jul 31, 2015
88
10
8
43
@EffrafaxOfWug Indeed that's the three-post RJ11 thing I'm using. :) Looking at wiki on protocols, VDSL2 seems to be the only one reaching download speeds of 100Mbit. Currently Speedtest.net is reporting 85Mbit down and 12Mbit up. That being said, this model might be worth looking into.

@RTM I'm not very familiar with scripting in bash, the only experience I have is some small scripts done in shell using tutorials. :) I do use Chrome, so I can take a look at requests in the developer tools.
 

TechIsCool

Active Member
Feb 8, 2012
263
117
43
Clinton, WA
techiscool.com
So on the web interface do you have a way to reboot the modem? Look around and see if you can find it. If there is is should be easy to do. The only problem your going to have is that if the modem does not respond to ping when it goes down you will have to use some type of Out of Band Management.
 

Solkku

Member
Jul 31, 2015
88
10
8
43
@TechIsCool Yes I do, but it needs a click of the mouse on the "Yes, restart my MediaAccess gateway" button on the page. If I try to directly go to the URL to which it points me to after I click that button, I get that "Access Error: 500" error like in the first screenshot of my first post.

So what I'm trying to accomplish is to emulate a click of that button with a script, since I can't use the direct URL of the reboot command (Access error: 500).
 

TechIsCool

Active Member
Feb 8, 2012
263
117
43
Clinton, WA
techiscool.com
Postman or Fiddler would work to watch what data is sent to the Modem and then you can script that. CURL is your friend for scripting to run things. If that is too confusing it might be simpler to go with a IP controlled Power Switch.
 

rubylaser

Active Member
Jan 4, 2013
846
236
43
Michigan, USA
Another option would be to call Mechanize(Python/Ruby) in a BASH script. The script would test if it can ping the internet (8.8.8.8 or any other public ip). If the ping times out, you would then run the mechanize script to navigate to the page and "push" the yes button to restart. If the ping gets a reply, the script would do noting and exit. You code run this via cronjob every 10 minutes.

This isn't super tricky to code up, but if you've never done any scripting before, I would go with the timer as suggested earlier since you go on vacation next week. Later, you can track down the Web Power Switch as suggested above, or investigate scripting further. These kinds of challenges are a great reason to learn to write scripts to solve problems like this.

Code:
sudo -i
mkdir -p /root/scripts
nano is_internet_available.sh
# paste...
Code:
#! /bin/sh

if ping -c 1 8.8.8.8 &> /dev/null
then
  echo "The internet seems to be down. I'll restart your modem now."
 # Run Mechanize script here...
  ruby /root/scripts/modem_restarter.rb
else
  echo "Nothing to do..."
fi
Press ctrl +x, and then "y", followed by Enter to save.

Code:
chmod +x /root/scripts/is_internet_available.sh
Here's what a Mechanize script looks like in Ruby (this is a simple file uploader I built for a web form).
Code:
require 'rubygems'
require 'mechanize'
require 'nokogiri'

# Uploads directory
@base_dir = '/uploads/'

# Find 4 most recent images in the base_dir
@photos = Dir.entries(base_dir).select{|f| File.file? "#{base_dir}#{f}" }.sort_by { |f| File.mtime("#{base_dir}#{f}") }.reverse[0..3]

# Upload each with Mechanize
agent = Mechanize.new
@photos.each do |photo|
  page = agent.get('http://example.test.com/photos/new')
  result = page.form_with(:method => /POST/) do |form|
    form.file_uploads.first.file_name = "@base_dir" + photo
  end.submit
end
 
Last edited:

Jeggs101

Well-Known Member
Dec 29, 2010
1,529
241
63
We had a really weird one where pfsense would not allow anything to ping even 8.8.8.8, but it could ping yahoo IPs whilst being unable to resolve yahoo.com.

What fixed: miracle, time and reboot. I think @rubylaser has the right idea.
 

Solkku

Member
Jul 31, 2015
88
10
8
43
@rubylaser Thanks for the tip! Just created a python play-around jail in NAS4Free and will test out mechanize (if I manage to find some noob-friendly examples / tutorials). :)
 

rubylaser

Active Member
Jan 4, 2013
846
236
43
Michigan, USA
@rubylaser Thanks for the tip! Just created a python play-around jail in NAS4Free and will test out mechanize (if I manage to find some noob-friendly examples / tutorials). :)
I just updated my example above to include an example of a simple BASH script that would call a Ruby Mechanize script (you could do all of this in Python/Ruby including the ping test, but BASH might be an easier starting example for you).

I also included a Ruby example of a mechanize script (Ruby is my primary coding language, so I'm sorry I didn't give you a Python example, but the concept is still the same).

Here's a good example, but it's for Ruby Writing a Web Crawler | The Bastards Book of Ruby
 
Last edited:

Solkku

Member
Jul 31, 2015
88
10
8
43

rubylaser

Active Member
Jan 4, 2013
846
236
43
Michigan, USA
Thank you @rubylaser

While you were helping me here I was playing around in the python shell and got this far: [Python] Python 2.7.10 (default, Aug 4 2015, 15:36:58) [GCC 4.2.1 20070831 patched [Fre - Pastebin.com

The button name='18' is which needs to be clicked it seems, but it didn't let me do it straight away (see output errors in pastebin). I wonder if the "onClick"-arguments are messing things up. Or that it's a type button and not submit.
Good job! You are making some progress. That is a submit button in a form. You need to get more specific and target that form by name "Restart" and then click the button. Your error is telling you it can't find the form.
Code:
mechanize._form.ControlNotFoundError: no control matching name '18', kind 'clickable'
Code:
br.select_form("Restart")
br.submit(name='18',  label="Yes, restart my MediaAccess Gateway")
 
Last edited: