📜 ⬆️ ⬇️

If the mountain does not go to Magomed ... How to maintain an active ADSL connection at the Krivorukov provider

It so happened that the negotiations with the provider that the connection sometimes disappears, and the ADSL modem does not notice this fact, did not lead to anything. I had to look for other ways.

Task : Programmatically restart the ADSL modem when the connection is lost in order to force a reset of the PPPoE session.

The solution below has been tested on the D-link DSL 2640U ADSL modem, but I think it can be easily adapted to any other models of ADSL modems.
')


So, the solution is a php script that every 5 minutes checks for a connection by pinging the provider's site.
If there is no ping, we send a reset command to the modem's ADSL.

Using Windows XP, it is quite problematic to run the script every 5 minutes. Therefore, Splinterware’s free System Scheduler was used as the System Sheduler.

For simplicity, suppose that denwer is installed on the computer and all files are located in Z: \ home \ inet-keeper \ www \

Decision:
In the above directory we have

1) an inet-keeper.bat file that runs every 5 minutes from System Scheduler
The inet-keeper.bat file runs the php script of the same name:
z:\usr\bin\php -n -f inet-keeper.php >> inet-keeper.log


2) inet-keeper.php file
content of inet-keeper.php file:
<?php
set_time_limit(60*5);

$res = exec("ping google.com -n 10", $output, $var);
$output = date('r');
$output .= " ## Ping result: $var"; // 0- 1-Error
$output .= " ## $res";
if ($var || substr_count($res, '= 0ms') == 3) // In case like Minimum = 0ms, Maximum = 0ms, Average = 0ms
{
$error = true;
$output .= " ## Error detected!";
$output .= "\nRebooting modem...\n";
reboot_modem();
}
$output .= "\n";

if ($error) // or true)
echo $output;

function reboot_modem()
{
$log = send_request("getlog");
file_put_contents("logs\\" . date('YM-d_H-i-s') . ".html" ,$log);
send_request("reboot");
}

function send_request($r) {
$login = "admin";
$password = "admin";
$auth = base64_encode($login . ":" . $password);
$fp = fsockopen('192.168.1.1', 80, $errno, $errstr, 60);
if ($fp)
{
switch ($r) {
case "reboot":
$out = "GET /rebootinfo.cgi HTTP/1.0\r\n";
break;
case "getlog":
$out = "GET /logview.cmd HTTP/1.0\r\n";
break;
default:
return;
}
$out .= "Host: 192.168.1.1\r\n";
$out .= "Authorization: Basic $auth\r\n";
$out .= "Connection: Close\r\n\r\n";
//
fwrite($fp, $out);
$html = "";

while (!feof($fp)){
$part=fgets($fp);
$html.=$part;
}
fclose($fp);
}
return $html;
}
?>


If the provider's site (in the example google.com) stops pinging, the script starts the modem to reboot by opening the page 192.168.1.1/rebootinfo.cgi

Since the modem requires authorization, there are lines in the send_request function that specify the login and password
$login = "admin";
$password = "admin";


When rebooting the modem will be
1) a record of this in the inet-keeper.log file
2) in the subdirectory log, the contents of the modem's system log page (in my case, the contents of the page 192.168.1.1/logview.cmd )

Source: https://habr.com/ru/post/96158/


All Articles