Changeset 239 for trunk/system/routerboard.php
- Timestamp:
- Aug 2, 2009, 10:28:58 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/routerboard.php
r161 r239 1 1 <?php 2 2 3 include_once('ssh.php'); 4 5 class Routerboard extends SSH 3 class Routerboard 6 4 { 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 } 12 18 13 19 function Execute($Commands) 14 { 20 { 15 21 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); 17 34 } 18 35 19 function GetItem($Command)36 function ItemGet($Path) 20 37 { 21 $Result = $this->Execute( $Command);38 $Result = $this->Execute(implode(' ', $Path).' print'); 22 39 array_pop($Result); 23 40 $List = array(); … … 25 42 { 26 43 $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)] = ''; 29 49 } 30 50 return($List); 31 51 } 32 52 33 function GetList($Command, $Properties)53 function ListGet($Path, $Properties, $Conditions = array()) 34 54 { 35 55 $PropertyList = '"'; 36 foreach($Properties as $ Property)56 foreach($Properties as $Index => $Property) 37 57 { 38 $PropertyList .= $ Property.'=".[get $i '.$Property.']." ';58 $PropertyList .= $Index.'=".[get $i '.$Property.']." '; 39 59 } 40 60 $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.')}}'); 42 70 $List = array(); 43 71 foreach($Result as $ResultLine) … … 48 76 { 49 77 $Value = explode('=', $ResultLinePart); 50 $ListItem[$ Value[0]] = $Value[1];78 $ListItem[$Properties[$Value[0]]] = $Value[1]; 51 79 } 52 80 $List[] = $ListItem; … … 55 83 } 56 84 57 function GetSystemResource()85 function ListEraseAll($Path) 58 86 { 59 return($this->GetItem('/system resource print'));87 $this->Execute(implode(' ', $Path).' { remove [find] }'); 60 88 } 61 62 function GetFirewallFilterList()89 90 function ListUpdate($Path, $Properties, $Values, $Condition = array()) 63 91 { 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)); 70 128 } 71 129 }
Note:
See TracChangeset
for help on using the changeset viewer.