PDA

Click to See Complete Forum and Search --> : [RESOLVED] Router Reset Script.


Slyke
May 23rd, 2009, 03:00 AM
AT my home I have a WAMP server running on a computer I have designated as a webserver.

Everyday I've got a script to restart the webserver at some wee hour in the morning.

I would also like to extend this to the router running the network. The router is good, but generally every 4 or so days it wants a reset. It's a LINKSYS WRT350N.

Basically, the process is as such with a browser:


Navigate to router
Place in login details (It's one of those htaccess login prompt boxes)
The first page that loads up has a "Save Settings" button which resets the router.


The first page is:
/setup.cgi?next_file=Setup.htm

The "Save Setting" button's code is:
<input type="BUTTON" name="save" value="Save Settings" onClick="checkData();">

The check data function is way too long, and is not needed to know (I believe).

The reset page is a loading screen. It takes roughly about a minute for the router to reset. Navigating directly to this page will cause the router to reset, but you have to still be authenticated in:
/setup.cgi?next_file=reboot_guage.htm

If some one could point me in the right direction on how to automate this process it would be greatly appreciated :D. I don't have any idea what to search for.

visualAd
May 23rd, 2009, 04:44 AM
My first initial reaction to that would be to buy a new router :D

How do you authenticate yourself on the router? Is it via a web page or does a authentication dialog pop up when you attempt to access it?

Slyke
May 24th, 2009, 02:12 AM
Yes, I know. But it's an awesome router :(. It just gets stuck after too much traffic runs through it (I have a media centre streaming some DVDs throughout the house, and it crashes usually a short time after it).

It's a dialog popup. Not one of those in page ones with a textbox and a submit button.

I attached a screenshot for you :).

visualAd
May 24th, 2009, 02:16 AM
It is using HTTP authentication. If you send along an authorization header in PHP (which the browser does with every request) then it should work.


header ('Authorization: ' . base64_encode($username . ':' . $password));

Slyke
May 24th, 2009, 03:37 AM
header ('Authorization: ' . base64_encode($username . ':' . $password));
header ('Location: /setup.cgi?next_file=reboot_guage.htm');

Something like that? Or does PHP support this type of login over multiple requests, or how to I place this in for every page PHP needs to navigate to? Not too sure how it works XD.

visualAd
May 24th, 2009, 04:12 AM
Silly me, i have got it the wrong way around. :confused:

You need to actually request the page manually using fsockopen and include the header within that. Have a look at the fsockopen page as it has an example of how to make an HTTP request directly:

http://uk2.php.net/fsockopen

Slyke
May 24th, 2009, 05:43 AM
<?php
$fp = fsockopen("192.168.1.1", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /setup.cgi?next_file=reboot_guage.htm HTTP/1.1\r\n";
$out .= "Host: 192.168.1.1\r\n";
$out .= 'Authorization: ' . base64_encode($username . ':' . $password) . '\r\n';
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
/*while (!feof($fp)) {
echo fgets($fp, 128);
// dnt need to see it :).
}*/
fclose($fp);
}
?>


I think I got it :)?

visualAd
May 24th, 2009, 05:49 AM
Did it work? You should at least read the first line of the response to check that it succeeded (i.e. you get a HCCP 200 OK).

Slyke
May 24th, 2009, 07:58 AM
Nahh, it didn't work. I'm just getting Wireshark now and going to spy on the headers to see the browser doing it :).

It gave me a 401.


HTTP/1.1 401 Unauthorized Server: Date: Sun, 24 May 2009 22:57:48 GMT WWW-Authenticate: Basic realm="Linksys WRT350N" Content-Type: text/html Connection: close
401 Unauthorized
Authorization required.

I tried:
$out .= 'Authorization: Basic ' . base64_encode($username . ':' . $password) . '\r\n';


As well.

Slyke
May 24th, 2009, 08:43 AM
Got it working :). It just wanted:

$out .= "Keep-Alive: 300\r\n";
$out .= "Connection: keep-alive\r\n";

Router is being difficult, lol.

Thanks for your help!