Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/php
- <?php
- function logit($s) {
- echo $s. "\n";
- }
- function dm200_connect() {
- // attempts to connect to modem
- // - will drop existing connection if one exists
- // - will pause if connection attempts are too close together
- // - tries connection indefinitely, only returns once successfully connected
- global $dm200_ip;
- global $dm200_fp;
- global $dm200_lastconnect;
- if (!isset($dm200_lastconnect)) $dm200_lastconnect = 0;
- if (isset($dm200_fp)) {
- logit("warning: dm200_connect(): closing existing open connection");
- fclose($dm200_fp);
- }
- $t = time() - $dm200_lastconnection;
- if ($t < 60) {
- $sleeptime = 60 - $t;
- logit("warning: dm200_connect(): sleeping $sleeptime due to recent connect attempt");
- sleep($sleeptime);
- }
- $sleeptime = 5;
- do { // try indefinitely
- $dm200_fp = fsockopen($dm200_ip, 23, $errno, $errstr, 5);
- if (!$dm200_fp) {
- logit("dm200_connect(): connect failed: $errstr: sleeping $sleeptime");
- sleep($sleeptime);
- $sleeptime += 10;
- if ($sleeptime > 120) $sleeptime = 120;
- }
- }
- while (!$dm200_fp);
- stream_set_timeout($dm200_fp, 5); // low timeout
- $dm200_lastconnect = time();
- return;
- }
- function dm200_waitfor($s) {
- // capture input and wait until $s appears in it, then return entire input
- // will return empty string if input times out, or connection closes
- global $dm200_fp;
- if (feof($dm200_fp)) return "";
- $r = "";
- $aborttime = time() + 5;
- do {
- $t = fread($dm200_fp, 8192);
- if ($t != "") {
- $r .= $t;
- } else {
- if (time() > $aborttime) return 0;
- usleep(50000); // 50ms
- }
- } while (strstr($r, $s) == "" && !feof($dm200_fp));
- return($r);
- }
- function dm200_send($s) {
- // send string. assumes connection is open
- global $dm200_fp;
- fputs($dm200_fp, $s);
- }
- if ($argv[1] == "") die("usage: $argv[0] router_ip\n");
- $dm200_ip = $argv[1];
- $dm200_username = "admin";
- $dm200_password = "password";
- $cmds[] = "uptime";
- $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997lsg 1 1"; // attainable rate down
- $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997lsg 0 1"; // attainable rate up
- $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997csg 0 1"; // actual rate down
- $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997csg 0 0"; // actual rate up
- while (1) {
- tryagain:
- dm200_connect();
- $t = dm200_waitfor("telnet account:");
- if (strstr($t, "telnet account:") == "") {
- logit("login failed at account prompt");
- goto tryagain;
- }
- // echo $t;
- dm200_send($dm200_username . "\n");
- $t = dm200_waitfor("telnet password:");
- if (strstr($t, "telnet password:") == "") {
- logit("login failed at password prompt");
- goto tryagain;
- }
- // echo $t;
- dm200_send($dm200_password . "\n");
- $t = dm200_waitfor("root@DM200:/#");
- if (strstr($t, "root@DM200:/#") == "") {
- logit("login failed waiting for shell prompt");
- goto tryagain;
- }
- while (1) {
- $nexttime = time() + 30;
- for ($i = 0; isset($cmds[$i]); $i++) {
- dm200_send($cmds[$i] . "\n");
- $t = dm200_waitfor("root@DM200:/#");
- if (strstr($t, "root@DM200:/#") == "") {
- logit("command execution \"" . $cmds[$i] . "\" failed or stalled");
- goto tryagain;
- }
- $a = explode("\n", $t);
- for ($j = 0; isset($a[$j]); $j++) {
- $t = trim($a[$j]);
- if ($t != $cmds[$i] && $t != "" && $t != "root@DM200:/#") {
- // $t contains the command output
- echo $t . "\n";
- }
- }
- }
- $sleeptime = $nexttime - time();
- if ($sleeptime > 0) sleep($sleeptime);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement