130 lines
2.9 KiB
PHP
130 lines
2.9 KiB
PHP
<?php
|
|
|
|
use PEAR2\Net\RouterOS;
|
|
use PEAR2\Net\Transmitter\NetworkStream;
|
|
|
|
require __DIR__."/mfRouteros_Item.php";
|
|
|
|
class mfRouteros {
|
|
|
|
private $hostname;
|
|
private $port;
|
|
private $use_ssl;
|
|
private $username;
|
|
private $password;
|
|
private $ros = false;
|
|
|
|
public function __construct($hostname, $username, $password, $port = 8729, $use_ssl = true) {
|
|
$this->hostname = $hostname;
|
|
$this->username = $username;
|
|
$this->password = $password;
|
|
|
|
$this->port = $port;
|
|
$this->use_ssl = $use_ssl;
|
|
}
|
|
|
|
private function _connect() {
|
|
$crypto = NetworkStream::CRYPTO_OFF;
|
|
if($this->use_ssl) {
|
|
$crypto = NetworkStream::CRYPTO_TLS;
|
|
}
|
|
|
|
$this->ros = new RouterOS\Client($this->hostname,$this->username,$this->password,$this->port,false,3, $crypto);
|
|
}
|
|
|
|
public function get($table, $filter = array()) {
|
|
if(!$this->ros) $this->_connect();
|
|
|
|
if(substr($table, 0, 1) != '/') {
|
|
$table = "/".$table;
|
|
}
|
|
|
|
$req = new RouterOS\Request("$table print");
|
|
|
|
$q = false;
|
|
if(count($filter)) {
|
|
foreach($filter as $name => $value) {
|
|
if($q === false) {
|
|
$q = RouterOS\Query::where($name,$value);
|
|
} else {
|
|
$q->andWhere(RouterOS\Query::where($name,$value));
|
|
}
|
|
}
|
|
$req->setQuery($q);
|
|
}
|
|
|
|
$responses = $this->ros->sendSync($req);
|
|
|
|
$answers = [];
|
|
foreach($responses as $response) {
|
|
$answer = new mfRouteros_Item($response);
|
|
if($answer->propcount()) {
|
|
$answers[] = $answer;
|
|
}
|
|
}
|
|
|
|
return $answers;
|
|
|
|
}
|
|
|
|
public function getFirst($table, $filter = array()) {
|
|
$answers = $this->get($table,$filter);
|
|
return array_shift($answers);
|
|
}
|
|
|
|
public function getLast($table, $filter = array()) {
|
|
$answers = $this->get($table,$filter);
|
|
return array_pop($answers);
|
|
}
|
|
|
|
|
|
public function set($table, $action, $set=array(), $filter = array()) {
|
|
if(!$this->ros) $this->_connect();
|
|
|
|
if(substr($table, 0, 1) != '/') {
|
|
$table = "/".$table;
|
|
}
|
|
|
|
$item = $this->getFirst($table, $filter);
|
|
$id = $item['.id'];
|
|
|
|
$req = new RouterOS\Request("$table $action");
|
|
|
|
$req->setArgument("numbers", $id);
|
|
if(count($set)) {
|
|
foreach($set as $name => $value) {
|
|
$req->setArgument($name, $value);
|
|
}
|
|
}
|
|
|
|
$resp = $this->ros->sendSync($req);
|
|
|
|
if($resp->getType() !== RouterOS\Response::TYPE_FINAL) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* accessors */
|
|
public function hostname($hostname=false) {
|
|
if($hostname) {
|
|
$this->hostname = $hostname;
|
|
}
|
|
return $this->hostname;
|
|
}
|
|
|
|
public function username($username=false) {
|
|
if($username) {
|
|
$this->username = $username;
|
|
}
|
|
return $this->username;
|
|
}
|
|
|
|
public function password($password=false) {
|
|
if($password) {
|
|
$this->password = $password;
|
|
}
|
|
return $this->password;
|
|
}
|
|
|
|
} |