něco o mně | fotogalerie | guestbook | download |

MikroTik SSH PHP Class

<?php

// Uživatel v MikroTiku musí mít alespoň práva ssh, read, write. 
// Pokud chcete například odeslat e-mail, je nutné ještě povolit test.

class _ssh
 {
   private $connection = NULL;
   private $port = 22;

   private $methods = array("kex" => "diffie-hellman-group1-sha1",
                            "client_to_server" => array("crypt" => "3des-cbc",
                                                        "comp" => "none"),
                            "server_to_client" => array("crypt" => "aes256-cbc,aes192-cbc,aes128-cbc",
                                                        "comp" => "none"));

   private $callbacks = array("disconnect" => "disconnect");


   function connect($host,$username,$password)
    {
      $this->connection = ssh2_connect($host,$this->port,$this->methods,array($this,$this->callbacks));

      if(!$this->connection)
       {
         echo "FAIL: unable to establish connection<br />\n";
       }
      else
       {
         if(!ssh2_auth_password($this->connection,$username,$password))
          {
            echo "FAIL: unable to authenticate<br />\n";
          }
         else
          {
            // echo "OK: logged in...<br />\n";
          }
        }

      return $this->connection;
    }

   function disconnect($reason,$message,$language)
    {
      $this->connection = NULL;
      printf("Server disconnected with reason code [%d] and message: %s<br />\n",$reason,$message);
    }

   function fingerprint()
    {
      return ssh2_fingerprint($this->connection,SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
    }



   function command($command)
    {
      if(!($stream = ssh2_exec($this->connection,$command,null,null,80,25)))
       {
         echo "FAIL: unable to execute command<br />\n";
       }
      else
       {
         stream_set_blocking($stream,true);
         $data = "";
         while($buf = fread($stream,4096)) $data .= $buf;
         fclose($stream);
       }

      return $data;
    }
 }


$ssh = new _ssh;

?>


<?php

// pole konfigurace MikroTiku
$CONFIG = array();
// oddělovač bloků/sekcí
$header = "-- %s --";


function ParseBasic($string)
 {
   global $CONFIG,$header;

   $lines = split("\n",$string);
   $regexp_header = "/^".sprintf($header,"(.*)")."$/i";
   $block = "";

   foreach ($lines as $line)
    {
      $line = trim($line);
      if($line == "") continue;  // prázdné řádky ignorujeme

      if(preg_match($regexp_header,$line,$matches))
       {
         // máme záhlaví sekce
         $block = $matches[1];
       }
      else
        switch ($block)
         {
           case "IDENT":
           case "CLOCK":
           case "NTP":
           case "LICENSE":
           case "ROUTERBOARD":
           case "RESOURCES":
           case "DNS":         
             //ParseVars(strtolower($block),$line);
           break;

           case "IP ADDRESS": ;
           case "HOTSPOT USERS":
           case "HOTSPOT PROFILES":
           case "WALLED GARDEN": ;
           case "DNS":
             //ParseDetailOutput(strtolower($block),$line);
           break;

           case "LOG":
             //ParseLog(strtolower($block),$line);
           break;
         }
    }
 }


$getdata = array("IDENT" => "system identity print",
                 "CLOCK" => "system clock print",
                 "NTP" => "system ntp client print",
                 "LICENSE" => "system license print",
                 "ROUTERBOARD" => "system routerboard print",
                 "RESOURCES" => "system resource print",
                 "IP ADDRESS" => "ip address print detail without-paging",
                 "ROUTES" => "ip route print detail without-paging",
                 "HOTSPOT USERS" => "ip hotspot user print detail without-paging",
                 "HOTSPOT PROFILES" => "ip hotspot user profile print detail without-paging",                 
                 "WALLED GARDEN" => "ip hotspot walled-garden print without-paging",
                 "DNS" => "ip dns print",
                 "LOG" => "log print without-paging");


$command = "";

foreach ($getdata as $key => $value)
 {
   $command .= ':put "'.sprintf($header,$key).'";';
   $command .= sprintf("%s;",$value);
 }


$ssh->connect("192.168.1.1","admin","pass");

$result = $ssh->command($command);
ParseBasic($result);


echo "<pre>";
var_dump($CONFIG);
echo "</pre>";

?>