Changeset 239 for trunk/system


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.
Location:
trunk/system
Files:
6 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/system/generators/dhcp_routerboard.php

    r213 r239  
    22
    33if(isset($_SERVER['REMOTE_ADDR'])) die();
    4 include_once('../../global.php');
     4include('../../global.php');
     5include('../routerboard.php');
     6$Path = array('ip', 'dhcp-server', 'lease');
     7
     8$Routerboard = new Routerboard();
     9$Routerboard->UserName = $Config['MainRouter']['UserName'];
     10$Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
    511
    612$DbResult = $Database->query('SELECT * FROM `NetworkSubnet` WHERE `Member` = 0');
     
    814{
    915  echo($Subnet['DHCP']);
     16  $Routerboard->HostName = $Subnet['DHCP'];
    1017  $I = explode('.', $Subnet['AddressRange']);
    1118  $I = $I[2];
    12   $Commands = array();
    13   $Commands[] = '/ip dhcp-server lease { remove [find server=dhcp'.$I.']}';
     19  $Items = array();
    1420  $DbResult2 = $Database->query('SELECT NetworkInterface.*, NetworkDevice.Name AS DeviceName FROM `NetworkInterface` LEFT JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device WHERE (`LocalIP` LIKE "192.168.'.$I.'.%") AND (`MAC` != "00:00:00:00:00:00") ORDER BY `LocalIP`');
    1521  while($Interface = $DbResult2->fetch_assoc())
    1622  {
    17     $Commands[] = '/ip dhcp-server lease add mac-address='.$Interface['MAC'].' address='.$Interface['LocalIP'].' server=dhcp'.$I.' comment='.$Interface['DeviceName'].'-'.$Interface['Name'];
     23    $Name = $Interface['DeviceName'];
     24    if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
     25    $Items[] = array('mac-address' => $Interface['MAC'], 'address' => $Interface['LocalIP'], 'server' => 'dhcp'.$I, 'comment' => $Name);
    1826  }
    19   $Commands = implode(';', $Commands);
    20   $Command = '/usr/bin/ssh -o ConnectTimeout=5 -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Subnet['DHCP'].' "'.$Commands.'"';
    21   //echo($Command."\n");
    22   $Output = '';
    23   exec($Command, $Output);
    24   print_r($Output);
     27 
     28  print_r($Routerboard->ListUpdate($Path, array('mac-address', 'address', 'server', 'comment'), $Items, array('server' => 'dhcp'.$I)));   
    2529  echo("\n");
    2630}
  • trunk/system/generators/netwatch.php

    r235 r239  
    22
    33if(isset($_SERVER['REMOTE_ADDR'])) die();
    4 include_once('../../global.php');
     4include('../../global.php');
     5include('../routerboard.php');
     6$Path = array('tool', 'netwatch');
     7
     8$Routerboard = new Routerboard();
     9$Routerboard->UserName = $Config['MainRouter']['UserName'];
     10$Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
    511
    612$DbResult3 = $Database->query('SELECT DISTINCT (`DHCP`) FROM `NetworkSubnet` WHERE `Member` = 0');
     
    814{
    915  echo($Router['DHCP']."\n");
    10   $Commands = array();
    11   $Commands[] = '/tool netwatch remove [find]';
     16  $Routerboard->HostName = $Router['DHCP'];
     17  $Items = array();
    1218  $DbResult = $Database->query('SELECT * FROM `NetworkSubnet` WHERE `DHCP`="'.$Router['DHCP'].'"');
    1319  while($Subnet = $DbResult->fetch_assoc())
     
    2228      if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
    2329      echo($Name.', ');
    24       $Commands[] = '/tool netwatch add host='.$Interface['LocalIP'].' interval=1m comment='.$Name;
     30      $Items[] = array('host' => $Interface['LocalIP'], 'interval' => '00:01:00', 'comment' => $Name);
    2531    }
    2632  }
    27   $Commands = implode(';', $Commands);
    28   $Command = '/usr/bin/ssh -o ConnectTimeout='.$Config['MainRouter']['ConnectTimeout'].' -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Router['DHCP'].' "'.$Commands.'"';
    29   //echo($Command."\n");
    30   $Output = '';
    31   exec($Command, $Output);
    32   print_r($Output);
     33  $Routerboard->ListUpdate($Path, array('host', 'interval', 'comment'), $Items);   
    3334  echo("\n");
    3435}
  • trunk/system/queues_import.php

    r161 r239  
    11<?php
    2 include_once('global.php');
     2include_once('../global.php');
    33
    44$Config['Web']['ShowErrors'] = 1;
    55
    6 include_once('routerboard.php');
     6include('routerboard.php');
    77
    88$Routerboard = new Routerboard($Config['MainRouter']['HostName'], $Config['MainRouter']['UserName'], $Config['MainRouter']['Password']);
    9 $QueueTree = $Routerboard->GetList('/queue tree', array('bytes', 'rate', 'parent', 'packet-mark', 'name'));
    10 print($QueueTree);
     9$Routerboard->PrivateKey = 'generators/id_dsa';
     10print_r($Routerboard->ListUpdate(array('tool', 'netwatch'), array('host', 'comment'), array()));
     11//print_r($Routerboard->Execute('ip address {:foreach i in=[find] do={:put "address=".[get $i address]." network=".[get $i network])}}'));
     12//print_r($Routerboard->GetItem('system ntp client print'));
    1113
    1214?>
  • 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.