Home > Articles

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Tying It All Together: Rebooting the Router

As I mentioned in part 1 of this series, my DSL modem/router has a reboot option in its web interface. By using the web interface, I quickly found the last HTML page before the reboot, as shown in Figure 1. The pertinent source code from this page is shown in Listing 2.

Figure 1

Figure 1 The "reboot now" screen of my DSL modem/router’s web interface.

Listing 2 The pertinent source code of the "reboot now" page.

<FORM name="myform" ACTION="/basic_setup_finish.html">

 <br><font size=4 face=verdana color=#000000><b>Save and Restart</b></font><p>
 <font face=verdana size=2><p>Please click the <b>Save and Restart</b> button
 below to save your settings and restart your Gateway.</font><p><br>

 <INPUT type="submit" name="Save and Restart" VALUE="Save and Restart">
</FORM>

A little HTML form decoding tells us that if the modem receives a request for the following HTML, it will reboot:

/basic_setup_finish.html?Save+and+Restart=Save+and+Restart

The <form> tag’s action attribute specifies the page to be requested, /basic_setup_finish.html in this example. The form components, in this case just the Submit button, have their data added to the end of the URL, beginning with a question mark (?), in the following format:

name=value

The <form> tag’s lack of a method attribute means that this form uses the GET method—that is, a GET request to the server with the form data appended to the URL.

In this case, the name and value of the submit button is Save and Restart, but since URLs cannot include spaces, each space becomes a plus (+). So the URL is constructed as shown in Figure 2.

Figure 2

Figure 2 The pieces of the document to be requested in order to reboot the router.

So that’s it. Simply request the document, with the appropriate form data appended to the URL, and include the correct Authorization header in the request. Cake, right? At this point it really is, depending on your scripting skills and the language in which you choose to script. Listing 3 shows a basic PHP script to reboot my DSL modem/router.

Listing 3 The final "reset router" script, resetactiontec.php.

<?php

// Get data from the open HTTP stream
function fromhttp ($fp) {
 $text = fgets($fp,4096);
 return $text;
}

// Send data to the open HTTP stream
function tohttp ($fp,$text) {
 fwrite($fp,$text);
}

// Assign user and password
$user = "hero";
$pass = "goat";

// Set server IP (DSL router IP)
$server = "192.168.0.1";

// URL to request
$url = "/basic_setup_finish.html?Save+and+Restart=Save+and+Restart";

// Create the Authorization header
$auth = "Authorization: Basic " .
 base64_encode($user . ":" . $pass);

$getrequest = <<<HTTP
GET $url HTTP/1.1
Accept: text/plain,text/html
Host: localhost
User-Agent: PHP
Connection: Close
$auth


HTTP;
// Note the two blank lines above; due to PHP heredoc idiosyncrasies,
// this creates one blank line in the data to be sent

// If we can open a connection (stream) to the router
if ($fp = fsockopen($server, 80, $errno, $errstr, 30)) {

 // Set our timeout appropriately
 stream_set_timeout ($fp,2);

 // Send the request to the HTTP stream
 tohttp($fp,$getrequest);

 // Get and print response from HTTP stream
 do {
 $temp = fromhttp($fp);
 print $temp;
 } while (!empty($temp));

 // Close the connection
 fclose($fp);

} else {

 // Report connection failure
 print "Could not open connection to router!";

}

?>

Notice that the script outputs the response received from the modem/router. That information allows me to run the PHP script from the command line and use my operating system scripting tools (the bash interpreter, since I use Linux) to interpret the results of the script (reboot, failed connection, etc.) and perform other actions accordingly. PHP just made it easy to open an HTTP stream and communicate with the router.

You could perform the same operation in a DOS batch file or UNIX/Linux shell script by using a prepared text file for the request and piping it to a Telnet application, similar to the following:

telnet 192.168.70.1 80 <authrequest.txt
  • + Share This
  • 🔖 Save To Your Account