[161] | 1 | <?php
|
---|
| 2 |
|
---|
[239] | 3 | class Routerboard
|
---|
[161] | 4 | {
|
---|
[239] | 5 | var $SSHPath = '/usr/bin/ssh';
|
---|
| 6 | var $Timeout = 3;
|
---|
| 7 | var $HostName;
|
---|
| 8 | var $UserName;
|
---|
| 9 | var $Password;
|
---|
| 10 | var $PrivateKey = 'id_dsa';
|
---|
[240] | 11 | var $MaxBurstLineCount = 100;
|
---|
[248] | 12 | var $Debug = false;
|
---|
| 13 |
|
---|
[239] | 14 | function __construct($HostName = 'localhost', $UserName = 'admin', $Password = '')
|
---|
| 15 | {
|
---|
| 16 | $this->HostName = $HostName;
|
---|
| 17 | $this->UserName = $UserName;
|
---|
| 18 | $this->Password = $Password;
|
---|
| 19 | }
|
---|
[161] | 20 |
|
---|
| 21 | function Execute($Commands)
|
---|
[240] | 22 | {
|
---|
| 23 | $Output = array();
|
---|
| 24 | if(is_array($Commands))
|
---|
| 25 | {
|
---|
| 26 | $I = 0;
|
---|
| 27 | $Batch = array();
|
---|
| 28 | while($I < count($Commands))
|
---|
| 29 | {
|
---|
| 30 | if(($I % $this->MaxBurstLineCount) == 0)
|
---|
| 31 | {
|
---|
| 32 | if(count($Batch) > 0)
|
---|
| 33 | $Output = array_merge($Output, $this->ExecuteBatch(implode(';', $Batch)));
|
---|
| 34 | $Batch = array();
|
---|
| 35 | }
|
---|
| 36 | $Batch[] = $Commands[$I];
|
---|
| 37 | $I++;
|
---|
| 38 | }
|
---|
| 39 | if(count($Batch) > 0)
|
---|
| 40 | $Output = array_merge($Output, $this->ExecuteBatch(implode(';', $Batch)));
|
---|
| 41 | } else
|
---|
| 42 | $Output = array_merge($Output, $this->ExecuteBatch($Commands));
|
---|
| 43 | return($Output);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | function ExecuteBatch($Commands)
|
---|
[239] | 47 | {
|
---|
| 48 | $Commands = trim($Commands);
|
---|
| 49 | if($Commands != '')
|
---|
| 50 | {
|
---|
| 51 | $Commands = addslashes($Commands);
|
---|
| 52 | $Commands = str_replace('$', '\$', $Commands);
|
---|
[317] | 53 | //$Commands = str_replace(' ', '\ ', $Commands);
|
---|
[239] | 54 | $Command = $this->SSHPath.' -o ConnectTimeout='.$this->Timeout.' -l '.$this->UserName.' -i '.$this->PrivateKey.' '.$this->HostName.' "'.$Commands.'"';
|
---|
[248] | 55 | if($this->Debug) echo($Command);
|
---|
[317] | 56 | $Output = array();
|
---|
[656] | 57 | //exec($Command, $Output);
|
---|
[239] | 58 | } else $Output = '';
|
---|
[248] | 59 | if($this->Debug) print_r($Output);
|
---|
[239] | 60 | return($Output);
|
---|
[161] | 61 | }
|
---|
| 62 |
|
---|
[239] | 63 | function ItemGet($Path)
|
---|
[161] | 64 | {
|
---|
[239] | 65 | $Result = $this->Execute(implode(' ', $Path).' print');
|
---|
[161] | 66 | array_pop($Result);
|
---|
| 67 | $List = array();
|
---|
| 68 | foreach($Result as $ResultLine)
|
---|
| 69 | {
|
---|
| 70 | $ResultLineParts = explode(' ', trim($ResultLine));
|
---|
[239] | 71 | if(count($ResultLineParts) > 1)
|
---|
| 72 | {
|
---|
| 73 | if($ResultLineParts[1]{0} == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
|
---|
| 74 | $List[substr($ResultLineParts[0], 0, -1)] = $ResultLineParts[1];
|
---|
| 75 | } else $List[substr($ResultLineParts[0], 0, -1)] = '';
|
---|
[161] | 76 | }
|
---|
| 77 | return($List);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[239] | 80 | function ListGet($Path, $Properties, $Conditions = array())
|
---|
[161] | 81 | {
|
---|
| 82 | $PropertyList = '"';
|
---|
[239] | 83 | foreach($Properties as $Index => $Property)
|
---|
[161] | 84 | {
|
---|
[239] | 85 | $PropertyList .= $Index.'=".[get $i '.$Property.']." ';
|
---|
[161] | 86 | }
|
---|
| 87 | $PropertyList = substr($PropertyList, 0, -3);
|
---|
[239] | 88 |
|
---|
| 89 | $ConditionList = '';
|
---|
| 90 | foreach($Conditions as $Index => $Item)
|
---|
| 91 | {
|
---|
[301] | 92 | if($Item == 'no') $ConditionList .= $Index.'='.$Item.' ';
|
---|
| 93 | else $ConditionList .= $Index.'="'.$Item.'" ';
|
---|
[239] | 94 | }
|
---|
| 95 | $ConditionList = substr($ConditionList, 0, -1);
|
---|
| 96 |
|
---|
| 97 | $Result = $this->Execute(implode(' ', $Path).' {:foreach i in=[find '.$ConditionList.'] do={:put ('.$PropertyList.')}}');
|
---|
[161] | 98 | $List = array();
|
---|
| 99 | foreach($Result as $ResultLine)
|
---|
| 100 | {
|
---|
| 101 | $ResultLineParts = explode(' ', $ResultLine);
|
---|
| 102 | $ListItem = array();
|
---|
| 103 | foreach($ResultLineParts as $ResultLinePart)
|
---|
| 104 | {
|
---|
| 105 | $Value = explode('=', $ResultLinePart);
|
---|
[240] | 106 | if(count($Value) > 1) $ListItem[$Properties[$Value[0]]] = $Value[1];
|
---|
| 107 | else $ListItem[$Properties[$Value[0]]] = '';
|
---|
[161] | 108 | }
|
---|
| 109 | $List[] = $ListItem;
|
---|
| 110 | }
|
---|
| 111 | return($List);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[241] | 114 | function ListGetPrint($Path, $Properties, $Conditions = array())
|
---|
| 115 | {
|
---|
| 116 | $ConditionList = '';
|
---|
| 117 | foreach($Conditions as $Index => $Item)
|
---|
| 118 | {
|
---|
| 119 | $ConditionList .= $Index.'="'.$Item.'" ';
|
---|
| 120 | }
|
---|
| 121 | $ConditionList = substr($ConditionList, 0, -1);
|
---|
| 122 | if(trim($ConditionList) != '')
|
---|
| 123 | $ConditionList = ' where '.$ConditionList;
|
---|
| 124 |
|
---|
| 125 | $Result = $this->Execute(implode(' ', $Path).' print terse'.$ConditionList);
|
---|
| 126 | $List = array();
|
---|
| 127 | foreach($Result as $ResultLine)
|
---|
| 128 | {
|
---|
| 129 | $ResultLineParts = explode(' ', $ResultLine);
|
---|
| 130 | $ListItem = array();
|
---|
| 131 | foreach($ResultLineParts as $ResultLinePart)
|
---|
| 132 | {
|
---|
| 133 | $Value = explode('=', $ResultLinePart);
|
---|
| 134 | if(in_array($Value[0], $Properties))
|
---|
| 135 | {
|
---|
| 136 | if(count($Value) > 1)
|
---|
| 137 | {
|
---|
| 138 | if($Value[1]{0} == '"') $Value[1] = substr($Value[1], 1, -1);
|
---|
| 139 | //if(strlen($Value[1]) > 0)
|
---|
| 140 | $ListItem[$Value[0]] = $Value[1];
|
---|
| 141 | } else $ListItem[$Value[0]] = '';
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | if(count($ListItem) > 0) $List[] = $ListItem;
|
---|
| 145 | }
|
---|
| 146 | return($List);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[239] | 149 | function ListEraseAll($Path)
|
---|
[161] | 150 | {
|
---|
[239] | 151 | $this->Execute(implode(' ', $Path).' { remove [find] }');
|
---|
[161] | 152 | }
|
---|
[239] | 153 |
|
---|
[241] | 154 | function ListUpdate($Path, $Properties, $Values, $Condition = array(), $UsePrint = false)
|
---|
[161] | 155 | {
|
---|
[260] | 156 | // Get current list from routerboard
|
---|
[242] | 157 | if($UsePrint == 0)
|
---|
| 158 | {
|
---|
| 159 | $List = $this->ListGet($Path, $Properties, $Condition);
|
---|
| 160 | // Change boolean values yes/no to true/false
|
---|
| 161 | foreach($List as $Index => $ListItem)
|
---|
| 162 | {
|
---|
| 163 | foreach($ListItem as $Index2 => $Item2)
|
---|
| 164 | {
|
---|
| 165 | if($Item2 == 'true') $List[$Index][$Index2] = 'yes';
|
---|
| 166 | if($Item2 == 'false') $List[$Index][$Index2] = 'no';
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | } else
|
---|
| 170 | {
|
---|
| 171 | $List = $this->ListGetPrint($Path, $Properties, $Condition);
|
---|
| 172 | }
|
---|
[239] | 173 | $Commands = array();
|
---|
| 174 |
|
---|
[240] | 175 | // Add empty properties to values
|
---|
| 176 | foreach($Values as $Index => $Item)
|
---|
| 177 | {
|
---|
| 178 | foreach($Properties as $Property)
|
---|
| 179 | {
|
---|
| 180 | if(!array_key_exists($Property, $Item))
|
---|
| 181 | $Item[$Property] = '';
|
---|
| 182 | }
|
---|
| 183 | $Values[$Index] = $Item;
|
---|
| 184 | }
|
---|
[241] | 185 | foreach($List as $Index => $Item)
|
---|
| 186 | {
|
---|
| 187 | foreach($Properties as $Property)
|
---|
| 188 | {
|
---|
| 189 | if(!array_key_exists($Property, $Item))
|
---|
| 190 | $Item[$Property] = '';
|
---|
| 191 | }
|
---|
| 192 | $List[$Index] = $Item;
|
---|
| 193 | }
|
---|
[240] | 194 |
|
---|
[241] | 195 | // Sort properties
|
---|
| 196 | foreach($Values as $Index => $Item)
|
---|
| 197 | {
|
---|
| 198 | ksort($Values[$Index]);
|
---|
| 199 | }
|
---|
| 200 | foreach($List as $Index => $Item)
|
---|
| 201 | {
|
---|
| 202 | ksort($List[$Index]);
|
---|
| 203 | }
|
---|
[261] | 204 | if($this->Debug) print_r($List);
|
---|
| 205 | if($this->Debug) print_r($Values);
|
---|
[242] | 206 |
|
---|
[239] | 207 | // Erase all items not existed in $Values
|
---|
| 208 | foreach($List as $Index => $ListItem)
|
---|
| 209 | {
|
---|
| 210 | if(!in_array($ListItem, $Values))
|
---|
| 211 | {
|
---|
| 212 | $Prop = '';
|
---|
| 213 | foreach($ListItem as $Index => $Property)
|
---|
| 214 | {
|
---|
[242] | 215 | if($Property != '')
|
---|
| 216 | {
|
---|
| 217 | if(($Property == 'yes') or ($Property == 'no')) $Prop .= $Index.'='.$Property.' ';
|
---|
| 218 | else $Prop .= $Index.'="'.$Property.'" ';
|
---|
| 219 | }
|
---|
[239] | 220 | }
|
---|
| 221 | $Prop = substr($Prop, 0, -1);
|
---|
[241] | 222 | if(trim($Prop) != '')
|
---|
| 223 | $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']';
|
---|
[239] | 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | // Add new items
|
---|
| 228 | foreach($Values as $ListItem)
|
---|
| 229 | {
|
---|
| 230 | if(!in_array($ListItem, $List))
|
---|
| 231 | {
|
---|
| 232 | $Prop = '';
|
---|
| 233 | foreach($ListItem as $Index => $Property)
|
---|
| 234 | {
|
---|
[240] | 235 | if($Property != '') $Prop .= $Index.'="'.$Property.'" ';
|
---|
[239] | 236 | }
|
---|
| 237 | $Prop = substr($Prop, 0, -1);
|
---|
| 238 | $Commands[] = implode(' ', $Path).' add '.$Prop;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
[248] | 241 | if($this->Debug) print_r($Commands);
|
---|
[239] | 242 | return($this->Execute($Commands));
|
---|
[161] | 243 | }
|
---|
| 244 | }
|
---|