Compal CH7466CE Automatic Restart
The CH7466CE is a cable modem and wireless router that came with our plan from our ISP. I found it needed a reboot every couple of days or connection speeds would be a bit sluggish.
The simplest way to reboot is to pull the plug and wait three minutes for it to restart. Alternatively here's a bit of Python code to automate the process and force the machine to reboot late in the night, when your connection isn't in use.
This uses Python 3.6 since f-strings (PEP 498) make things a bit more elegant. Only dependency is Requests - again for the sake of elegance and simplicity.
First let's define our constants. I've left default router settings in place, but you'll want to change these as needed.
import requests
ROUTER_IP = '192.168.0.1'
LOGIN_URI = f'http://{ROUTER_IP}/common_page/login.html'
SETTER_URI = f'http://{ROUTER_IP}/xml/setter.xml'
USERNAME = 'admin'
PASSWORD = 'password'
Next we're going to create a basic Connection class. It will instantiate a new Requests session to be used through the process and will define two tokens which will later be provided by the router.
class Connection:
def __init__(self):
self.session = requests.Session()
self.user_token = ''
self.admin_token = ''
We'll hit the server with a GET request and extract the data from a sessionToken cookie. We'll need this data before progressing any further.
class Connection:
....
def set_user_token(self):
response = self.session.get(LOGIN_URI)
if response.status_code == 200:
print('Connected to login page.')
self.user_token = response.cookies.get('sessionToken')
We've saved the sessionToken data to our user_token variable. Now let's wrap up this data along with the login and password. This bundle will be sent as JSON in a POST to the router's webserver.
class Connection:
....
def set_admin_token(self):
post_params = [('token', self.user_token), ('fun', '15'),
('Username', USERNAME), ('Password', PASSWORD)]
response = self.session.post(SETTER_URI, data=post_params)
if response.text == 'successful':
print('Successfully logged into router.')
self.admin_token = response.cookies.get('sessionToken')
If the login succeeds, the server will provide us with a new sessionToken. Using this new token will give us elevated permissions for a variety of functions. Adding 'fun': '15' a JSON field here will reboot the router. Other numbers perform different functions - many of which do not require admin access but still display potentially sensitive router information or reset the router back its factory default.
class Connection:
....
def reboot(self):
post_params = [('token', self.admin_token), ('fun', '8')]
self.session.post(SETTER_URI, data=post_params)
print('Rebooting router. Please wait about 2.5 minutes.')
The final steps needed are here:
if __name__ == '__main__':
c = Connection()
c.set_user_token()
c.set_admin_token()
c.reboot()
If on Windows, you can use Task Scheduler to run this at a specific time at regular intervals. If on Linux/Mac, you can run crontab -e from bash prompt and add something like this:
0 4 * * * python3 /path/to/script.py