Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. #!/usr/local/bin/php
  2. <?php
  3.  
  4. function logit($s) {
  5.   echo $s. "\n";
  6. }
  7.  
  8. function dm200_connect() {
  9.  
  10. // attempts to connect to modem
  11. // - will drop existing connection if one exists
  12. // - will pause if connection attempts are too close together
  13. // - tries connection indefinitely, only returns once successfully connected
  14.  
  15.   global $dm200_ip;
  16.   global $dm200_fp;
  17.   global $dm200_lastconnect;
  18.  
  19.   if (!isset($dm200_lastconnect))  $dm200_lastconnect = 0;
  20.  
  21.   if (isset($dm200_fp)) {
  22.     logit("warning: dm200_connect(): closing existing open connection");
  23.     fclose($dm200_fp);
  24.   }
  25.  
  26.   $t = time() - $dm200_lastconnection;
  27.   if ($t < 60) {
  28.     $sleeptime = 60 - $t;
  29.     logit("warning: dm200_connect(): sleeping $sleeptime due to recent connect attempt");
  30.     sleep($sleeptime);
  31.   }
  32.  
  33.   $sleeptime = 5;
  34.   do {  // try indefinitely
  35.     $dm200_fp = fsockopen($dm200_ip, 23, $errno, $errstr, 5);
  36.     if (!$dm200_fp) {
  37.       logit("dm200_connect(): connect failed: $errstr: sleeping $sleeptime");
  38.       sleep($sleeptime);
  39.       $sleeptime += 10;
  40.       if ($sleeptime > 120)  $sleeptime = 120;
  41.     }
  42.   }
  43.   while (!$dm200_fp);
  44.   stream_set_timeout($dm200_fp, 5);  // low timeout
  45.   $dm200_lastconnect = time();
  46.   return;
  47. }
  48.  
  49. function dm200_waitfor($s) {
  50.  
  51. // capture input and wait until $s appears in it, then return entire input
  52. // will return empty string if input times out, or connection closes
  53.  
  54.   global $dm200_fp;
  55.  
  56.   if (feof($dm200_fp))  return "";
  57.   $r = "";
  58.   $aborttime = time() + 5;
  59.   do {
  60.     $t = fread($dm200_fp, 8192);
  61.     if ($t != "") {
  62.       $r .= $t;
  63.     } else {
  64.       if (time() > $aborttime)  return 0;
  65.       usleep(50000);  // 50ms
  66.     }
  67.   } while (strstr($r, $s) == "" && !feof($dm200_fp));
  68.   return($r);
  69. }
  70.  
  71. function dm200_send($s) {
  72.  
  73. // send string. assumes connection is open
  74.  
  75.   global $dm200_fp;
  76.  
  77.   fputs($dm200_fp, $s);
  78.  
  79. }
  80.  
  81.  
  82.  
  83. if ($argv[1] == "")  die("usage: $argv[0] router_ip\n");
  84.  
  85. $dm200_ip = $argv[1];
  86. $dm200_username = "admin";
  87. $dm200_password = "password";
  88.  
  89. $cmds[] = "uptime";
  90. $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997lsg 1 1";  // attainable rate down
  91. $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997lsg 0 1";  // attainable rate up
  92. $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997csg 0 1";  // actual rate down
  93. $cmds[] = "/opt/lantiq/bin/dsl_cpe_pipe.sh g997csg 0 0";  // actual rate up
  94.  
  95. while (1) {
  96. tryagain:
  97.   dm200_connect();
  98.   $t = dm200_waitfor("telnet account:");
  99.   if (strstr($t, "telnet account:") == "") {
  100.     logit("login failed at account prompt");
  101.     goto tryagain;
  102.   }
  103. //  echo $t;
  104.   dm200_send($dm200_username . "\n");
  105.   $t = dm200_waitfor("telnet password:");
  106.   if (strstr($t, "telnet password:") == "") {
  107.     logit("login failed at password prompt");
  108.     goto tryagain;
  109.   }
  110. //  echo $t;
  111.   dm200_send($dm200_password . "\n");
  112.   $t = dm200_waitfor("root@DM200:/#");
  113.   if (strstr($t, "root@DM200:/#") == "") {
  114.     logit("login failed waiting for shell prompt");
  115.     goto tryagain;
  116.   }
  117.   while (1) {
  118.     $nexttime = time() + 30;
  119.     for ($i = 0; isset($cmds[$i]); $i++) {
  120.       dm200_send($cmds[$i] . "\n");
  121.       $t = dm200_waitfor("root@DM200:/#");
  122.       if (strstr($t, "root@DM200:/#") == "") {
  123.         logit("command execution \"" . $cmds[$i] . "\" failed or stalled");
  124.         goto tryagain;
  125.       }
  126.       $a = explode("\n", $t);
  127.       for ($j = 0; isset($a[$j]); $j++) {
  128.         $t = trim($a[$j]);
  129.         if ($t != $cmds[$i] && $t != "" && $t != "root@DM200:/#")  {
  130. // $t contains the command output
  131.           echo $t . "\n";
  132.         }
  133.       }
  134.     }
  135.     $sleeptime = $nexttime - time();
  136.     if ($sleeptime > 0)  sleep($sleeptime);
  137.   }
  138. }
  139.  
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement