plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Help to build a script to scedule reboot to router

Tue Jun 28, 2016 11:19 am

I would like your much aprreciated help.

I have a ISP router (gateway) that doesen't have a schedule reboot on the web interface. I have a raspbian and I would like to schedule on crontab to reboot this router. I have google it and I need your help to do it with Elinks... Cause I don't have a clue...

Follow what I need to do, in order to reboot from a browser:

1 - open a browser and introduce the address remoterouter2016.dyndns.org or 192.168.1.1
(introduce username and password) than click apply
2 - remoterouter2016.dyndns.org/RgSetup.asp or 192.168.1.1/RgSetup.asp and click reboot
3 - after click reboot, pop up and ask if we really want or not, I need to click ok

The "big" problem is that this router/gateway is build with webinterface as asp (active server page) and for that, dosen't give me the proper link to reboot...

pcmanbob
Posts: 9465
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help to build a script to scedule reboot to router

Tue Jun 28, 2016 5:25 pm

HI.

I don't know how to write a script to do this but another option might be to use the pi to disconnect the power from the router for a few seconds that would preform the same function.
It might also be easier to set up to as you only need to get the pi to switch a relay which you could do with a simple python program which you can call with cron.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Tue Jun 28, 2016 6:41 pm

I aprecciate your answer, but it's not what I'm looking for :(

I would like something wiht wget, curl or Elinks

User avatar
topguy
Posts: 6491
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Help to build a script to scedule reboot to router

Tue Jun 28, 2016 8:53 pm

You could check out a tool called "xdotool" which you could use from a script to manipulate the browser, simulate mouse clicks even.
The trick would be to open the browser at a known location and size so you can predict location of the button you want to press.

If you want to only use "curl" and such you have to look at the source for the page and figure out what happens when you press the reboot button.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Help to build a script to scedule reboot to router

Tue Jun 28, 2016 9:19 pm

plopes1960 wrote:dosen't give me the proper link to reboot...
Does the router support ssh or even just telnet? If it supports this, you may find the router is just a read only linux that you can then directly run reboot on once you've logged in with your script.

You can then log in using an expect script or something ssh or if it is telnet and you just want to reboot:

Code: Select all

#!/bin/bash
{
        sleep 2s
        echo myUser
        sleep 2s
        echo myPassword
        sleep 2s
        echo sh
        sleep 5s
        echo ls
        echo reboot now
        sleep 2s
} | telnet myRouter
Exactly what you should do and can do will depend on your router.

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Tue Jun 28, 2016 9:43 pm

This router is a official router from ISP and don't accept Telnet or ssh connections.

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: Help to build a script to scedule reboot to router

Wed Jun 29, 2016 12:29 am

Python Selenium
Account Inactive

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Wed Jun 29, 2016 8:55 pm

I'm running raspbian Jessie on rpi 2.

I have manage more or less to build a python file under selenium ide on Firefox browser.

What I need to install on raspbian Jessie in order to run this Python file?

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: Help to build a script to scedule reboot to router

Wed Jun 29, 2016 9:17 pm

Well, since you are running the Full Raspbian Jessie with a desktop environment, the answer to "What needs to be installed" is, probably not much.

Can you post the python scrip that you have created, with the import lines, and if you'd like you can redact the username and password used to log in to the web interface of the router. Don't worry about redacting RFC 1918 addresses, they aren't routable and aren't a security concern.

You should have most of the hard work done. Now it's just getting the browser to render and then to figure out what kind of schedule you need to fire the script on.
Account Inactive

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Thu Jun 30, 2016 10:04 am

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://192.168.1.1/" />
<title>demonstration1</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">demonstration1</td></tr>
</thead><tbody>
<tr>
	<td>open</td>
	<td>/</td>
	<td></td>
</tr>
<tr>
	<td>type</td>
	<td>name=username</td>
	<td>Home_admin</td>
</tr>
<tr>
	<td>type</td>
	<td>//tr[3]/td[2]/input</td>
	<td>passadmin</td>
</tr>
<tr>
	<td>clickAndWait</td>
	<td>link=Ok</td>
	<td></td>
</tr>
<tr>
	<td>clickAndWait</td>
	<td>link=Reiniciar</td>
	<td></td>
</tr>
<tr>
	<td>clickAndWait</td>
	<td>//td[@id='center_ro_submit']/span/table/tbody/tr/td[2]</td>
	<td></td>
</tr>

</tbody></table>
</body>
</html>

when I run as root, give me this error

Code: Select all


./demonstration1.py: line 1: syntax error near unexpected token `newline'
./demonstration1.py: line 1: `<?xml version="1.0" encoding="UTF-8"?>'
I didn't install nothing yet, only run this file

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Thu Jun 30, 2016 4:27 pm

Code: Select all

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Destavez(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://192.168.1.1/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_destavez(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_name("username").clear()
        driver.find_element_by_name("username").send_keys("Home_admin")
        driver.find_element_by_xpath("//tr[3]/td[2]/input").clear()
        driver.find_element_by_xpath("//tr[3]/td[2]/input").send_keys("passadmin")
        driver.find_element_by_link_text("Ok").click()
        driver.find_element_by_link_text("Reiniciar").click()
        driver.find_element_by_link_text("Ok").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

I have put this file on

/var/script, than

Code: Select all

python destave.py
and give me this errors

Code: Select all

root@raspberrypi:/var/script# python destavez.py
E
======================================================================
ERROR: test_destavez (__main__.Destavez)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "destavez.py", line 12, in setUp
    self.driver = webdriver.Firefox()
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 80, in __init__
    self.binary, timeout)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 52, i                    n __init__
    self.binary.launch_browser(self.profile, timeout=timeout)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in laun                    ch_browser
    self._wait_until_connectable(timeout=timeout)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 99, in _wai                    t_until_connectable
    "The browser appears to have exited "
WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a lo                    g_file in the FirefoxBinary constructor, check it for details.


epoch1970
Posts: 5132
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Help to build a script to scedule reboot to router

Thu Jun 30, 2016 5:01 pm

Perhaps you could try with lynx. It has a recording mode, and a playback mode.
If your web interface is not too incompatible...
http://blog.unixy.net/2009/06/script-to ... sing-lynx/
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Fri Jul 01, 2016 8:19 am

Lynx - unsuportted URL scheme

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Fri Jul 01, 2016 3:32 pm

Any further help regarding Selenium Python?

Thanks in advance

plopes1960
Posts: 9
Joined: Tue Jun 28, 2016 11:13 am

Re: Help to build a script to scedule reboot to router

Sun Jul 03, 2016 4:58 am

Any help please?!??

Running Python Selenium on a raspbian?

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Help to build a script to scedule reboot to router

Sun Jul 03, 2016 10:21 am

I think we'd need more details of your router to be able to help you.

Have you thought about switching a relay with a GPIO and using that relay to cut/restore power to your router? Also have you thought about ditching selenium and using beautiful soup 4 to parse the HTML emitted from your router. BS4 is more commonly used and therefore better supported.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “General programming discussion”