Ignore:
Timestamp:
Aug 2, 2009, 10:28:58 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Vylepšena třída Routerboard pro načítání z/do nastavení do routerů Mikrotik.
  • Upraveno: Skripty pro generování DHCP a Netwatch routerů byly upraveny pro použití třídy Routerboar.
  • Upraveno: Zobrazování editačních políček formuláře lépe viditelné v IE8.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/system/routerboard.php

    r161 r239  
    11<?php
    22
    3 include_once('ssh.php');
    4 
    5 class Routerboard extends SSH
     3class Routerboard
    64{
    7   var $Methods = array(
    8     'kex' => 'diffie-hellman-group1-sha1',
    9     'client_to_server' => array('crypt' => '3des-cbc', 'comp' => 'none'),
    10     'server_to_client' => array('crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc', 'comp' => 'none')
    11   );
     5  var $SSHPath = '/usr/bin/ssh';
     6  var $Timeout = 3;
     7  var $HostName;
     8  var $UserName;
     9  var $Password;
     10  var $PrivateKey = 'id_dsa';
     11 
     12  function __construct($HostName = 'localhost', $UserName = 'admin', $Password = '')
     13  {
     14    $this->HostName = $HostName;
     15    $this->UserName = $UserName;
     16    $this->Password = $Password;
     17  }
    1218
    1319  function Execute($Commands)
    14   {
     20  {   
    1521    if(is_array($Commands)) $Commands = implode(';', $Commands);
    16     return(parent::Execute($Commands));
     22    $Commands = trim($Commands);
     23    if($Commands != '')
     24    {
     25      $Output = array();
     26      $Commands = addslashes($Commands);
     27      $Commands = str_replace('$', '\$', $Commands);
     28      $Command = $this->SSHPath.' -o ConnectTimeout='.$this->Timeout.' -l '.$this->UserName.' -i '.$this->PrivateKey.' '.$this->HostName.' "'.$Commands.'"';
     29      //echo($Command);
     30      exec($Command, $Output);
     31    } else $Output = '';
     32    //print_r($Output);
     33    return($Output);
    1734  }
    1835
    19   function GetItem($Command)
     36  function ItemGet($Path)
    2037  {
    21     $Result = $this->Execute($Command);
     38    $Result = $this->Execute(implode(' ', $Path).' print');
    2239    array_pop($Result);
    2340    $List = array();
     
    2542    {
    2643      $ResultLineParts = explode(' ', trim($ResultLine));
    27       if($ResultLineParts[1]{0} == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
    28       $List[substr($ResultLineParts[0], 0, -1)] = $ResultLineParts[1];
     44      if(count($ResultLineParts) > 1)
     45      {
     46        if($ResultLineParts[1]{0} == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
     47        $List[substr($ResultLineParts[0], 0, -1)] = $ResultLineParts[1];
     48      } else $List[substr($ResultLineParts[0], 0, -1)] = '';
    2949    }
    3050    return($List);
    3151  }
    3252
    33   function GetList($Command, $Properties)
     53  function ListGet($Path, $Properties, $Conditions = array())
    3454  {
    3555    $PropertyList = '"';
    36     foreach($Properties as $Property)
     56    foreach($Properties as $Index => $Property)
    3757    {
    38       $PropertyList .= $Property.'=".[get $i '.$Property.']." ';
     58      $PropertyList .= $Index.'=".[get $i '.$Property.']." ';
    3959    }
    4060    $PropertyList = substr($PropertyList, 0, -3);
    41     $Result = $this->Execute($Command.' {:foreach i in=[find] do={:put ('.$PropertyList.')}}');
     61
     62    $ConditionList = '';
     63    foreach($Conditions as $Index => $Item)
     64    {
     65      $ConditionList .= $Index.'="'.$Item.'" ';
     66    }
     67    $ConditionList = substr($ConditionList, 0, -1);
     68
     69    $Result = $this->Execute(implode(' ', $Path).' {:foreach i in=[find '.$ConditionList.'] do={:put ('.$PropertyList.')}}');
    4270    $List = array();
    4371    foreach($Result as $ResultLine)
     
    4876      {
    4977        $Value = explode('=', $ResultLinePart);
    50         $ListItem[$Value[0]] = $Value[1];
     78        $ListItem[$Properties[$Value[0]]] = $Value[1];
    5179      }
    5280      $List[] = $ListItem;
     
    5583  }
    5684
    57   function GetSystemResource()
     85  function ListEraseAll($Path)
    5886  {
    59     return($this->GetItem('/system resource print'));
     87    $this->Execute(implode(' ', $Path).' { remove [find] }');
    6088  }
    61 
    62   function GetFirewallFilterList()
     89 
     90  function ListUpdate($Path, $Properties, $Values, $Condition = array())
    6391  {
    64     return($this->GetList('/ip firewall nat', array('src-address', 'dst-address', 'bytes')));
    65   }
    66 
    67   function GetDHCPServerLeasesList()
    68   {
    69     return($this->GetList('/ip dhcp-server lease', array('address', 'active-address', 'comment', 'lease-time', 'status', 'host-name')));
     92    $List = $this->ListGet($Path, $Properties, $Condition);
     93    //print_r($List);
     94    //print_r($Values);
     95    $Commands = array();
     96   
     97    // Erase all items not existed in $Values
     98    foreach($List as $Index => $ListItem)
     99    {
     100      if(!in_array($ListItem, $Values))
     101      {
     102        $Prop = '';
     103        foreach($ListItem as $Index => $Property)
     104        {
     105          $Prop .= $Index.'="'.$Property.'" ';
     106        }
     107        $Prop = substr($Prop, 0, -1);
     108        $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']';
     109      }
     110    }
     111   
     112    // Add new items
     113    foreach($Values as $ListItem)
     114    {
     115      if(!in_array($ListItem, $List))
     116      {
     117        $Prop = '';
     118        foreach($ListItem as $Index => $Property)
     119        {
     120          $Prop .= $Index.'='.$Property.' ';
     121        }
     122        $Prop = substr($Prop, 0, -1);
     123        $Commands[] = implode(' ', $Path).' add '.$Prop;
     124      }
     125    }
     126    //print_r($Commands);
     127    return($this->Execute($Commands));   
    70128  }
    71129}
Note: See TracChangeset for help on using the changeset viewer.