Exemple #1
0
 public function testSendSyncReturningResponseLargeDataException()
 {
     //Required for this test
     $memoryLimit = ini_set('memory_limit', -1);
     try {
         $comment = fopen('php://temp', 'r+b');
         $fillerString = str_repeat('t', 0xffffff);
         //fwrite($comment, $fillerString);
         for ($i = 0; $i < 256; $i++) {
             fwrite($comment, $fillerString);
         }
         unset($fillerString);
         fwrite($comment, str_repeat('t', 0xfe));
         $argL = (double) sprintf('%u', ftell($comment));
         rewind($comment);
         $maxArgL = 0xffffffff - strlen('?comment=');
         $this->assertGreaterThan($maxArgL, $argL, '$comment is not long enough.');
         rewind($comment);
         $printRequest = new Request('/ip/arp/print');
         $printRequest->setQuery(Query::where('comment', $comment));
         $this->object->sendSync($printRequest);
         fclose($comment);
         //Clearing out for other tests.
         ini_set('memory_limit', $memoryLimit);
         $this->fail('Lengths above 0xFFFFFFFF should not be supported.');
     } catch (LengthException $e) {
         fclose($comment);
         //Clearing out for other tests.
         ini_set('memory_limit', $memoryLimit);
         $this->assertEquals(LengthException::CODE_UNSUPPORTED, $e->getCode(), 'Improper exception thrown.');
     }
 }
 /**
  * @depends testExecArgTypes
  * 
  * @return void
  */
 public function testExecArgValues()
 {
     $printRequest = new Request('/queue/simple/print', Query::where('name', TEST_QUEUE_NAME));
     $this->util->setMenu('/queue/simple');
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => 2));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('2', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => 'test'));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('test', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => true));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('true', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=[:pick $comment 0]', array('name' => TEST_QUEUE_NAME, 'comment' => array('hello', 'world')));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('hello', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => array()));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame(null, $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => null));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame(null, $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => new DateTime('1970-01-01 00:00:00.000001', new DateTimezone('UTC'))));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('00:00:00.000001', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => new DateTime('1970-01-02 00:00:01', new DateTimezone('UTC'))));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('1d00:00:01', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => new DateTime('1970-01-10 01:02:03', new DateTimezone('UTC'))));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('1w2d01:02:03', $results->getProperty('comment'));
     $this->util->exec('add name=$name target=0.0.0.0/0 comment=$comment', array('name' => TEST_QUEUE_NAME, 'comment' => new DateInterval('P8D')));
     $results = $this->client->sendSync($printRequest)->getAllOfType(Response::TYPE_DATA);
     $this->util->remove(TEST_QUEUE_NAME);
     $this->assertCount(1, $results);
     $this->assertSame('1w1d00:00:00', $results->getProperty('comment'));
 }
 public function testSetCharset()
 {
     if (!extension_loaded('iconv') || !function_exists('iconv')) {
         $this->markTestSkipped('iconv is not enabled.');
     }
     $this->assertEquals(array(Communicator::CHARSET_REMOTE => null, Communicator::CHARSET_LOCAL => null), $this->object->setCharset(array(Communicator::CHARSET_REMOTE => 'windows-1251', Communicator::CHARSET_LOCAL => 'UTF-8')));
     $addRequest = new Request('/queue/simple/add');
     $addRequest->setArgument('name', TEST_QUEUE_NAME)->setArgument('target', '0.0.0.0/0');
     $addRequest->setArgument('comment', 'ПРИМЕР');
     $responses = $this->object->sendSync($addRequest);
     $this->assertEquals(1, count($responses), 'There should be only one response.');
     if (count($responses) === 1 && $responses[-1]->getType() === Response::TYPE_FINAL) {
         $appropriateCharsets = $this->object->setCharset(array(Communicator::CHARSET_REMOTE => 'ISO-8859-1', Communicator::CHARSET_LOCAL => 'UTF-8'));
         $printRequest = new Request('/queue/simple/print');
         $printRequest->setQuery(Query::where('name', TEST_QUEUE_NAME));
         $responses = $this->object->sendSync($printRequest);
         $this->assertEquals(TEST_QUEUE_NAME, $responses[0]->getProperty('name'));
         $this->assertNotEquals('ПРИМЕР', $responses[0]->getProperty('comment'));
         $this->object->setCharset($appropriateCharsets);
         $this->assertNotEquals('ПРИМЕР', $responses[0]->getProperty('comment'));
         $responses = $this->object->sendSync($printRequest);
         $this->assertEquals(TEST_QUEUE_NAME, $responses[0]->getProperty('name'));
         $this->assertEquals('ПРИМЕР', $responses[0]->getProperty('comment'));
         $this->object->setCharset('ISO-8859-1', Communicator::CHARSET_REMOTE);
         $responses = $this->object->sendSync($printRequest);
         $this->assertNotEquals('ПРИМЕР', $responses[0]->getProperty('comment'));
         $this->object->setCharset('ISO-8859-1', Communicator::CHARSET_LOCAL);
         $responses = $this->object->sendSync($printRequest);
         $this->assertNotEquals('ПРИМЕР', $responses[0]->getProperty('comment'));
         $this->object->setCharset($appropriateCharsets);
         $responses = $this->object->sendSync($printRequest);
         $this->assertEquals('ПРИМЕР', $responses[0]->getProperty('comment'));
         $this->object->setStreamingResponses(true);
         $responses = $this->object->sendSync($printRequest);
         $this->assertEquals('ПРИМЕР', stream_get_contents($responses[0]->getProperty('comment')));
         $this->object->setCharset('ISO-8859-1', Communicator::CHARSET_REMOTE);
         $responses = $this->object->sendSync($printRequest);
         $this->assertNotEquals('ПРИМЕР', stream_get_contents($responses[0]->getProperty('comment')));
         $this->object->setCharset('windows-1251');
         $responses = $this->object->sendSync($printRequest);
         $this->assertNotEquals('ПРИМЕР', stream_get_contents($responses[0]->getProperty('comment')));
         $testQueueNameStream = fopen('php://temp', 'r+b');
         fwrite($testQueueNameStream, TEST_QUEUE_NAME);
         rewind($testQueueNameStream);
         $printRequest->setQuery(Query::where('name', $testQueueNameStream));
         $responses = $this->object->sendSync($printRequest);
         $this->assertNotEquals('ПРИМЕР', stream_get_contents($responses[0]->getProperty('comment')));
         $this->object->setCharset($appropriateCharsets);
         $responses = $this->object->sendSync($printRequest);
         $this->assertEquals('ПРИМЕР', stream_get_contents($responses[0]->getProperty('comment')));
         $removeRequest = new Request('/queue/simple/remove');
         $removeRequest->setArgument('numbers', TEST_QUEUE_NAME);
         $responses = $this->object->sendSync($removeRequest);
     }
 }
 protected function getVersion($host, $username, $password)
 {
     try {
         $client = new RouterOS\Client($host, $username, $password);
     } catch (Exception $e) {
         die('Unable to connect to the router.');
         //Inspect $e if you want to know details about the failure.
     }
     $responses = $client->sendSync(new RouterOS\Request('/system/resource/print'));
     foreach ($responses as $response) {
         if ($response->getType() === RouterOS\Response::TYPE_DATA) {
             return $response->getProperty('version');
         }
     }
     //
 }
 protected function getResources($host, $username, $password)
 {
     try {
         $client = new RouterOS\Client($host, $username, $password);
     } catch (Exception $e) {
         die('Unable to connect to the router.');
         //Inspect $e if you want to know details about the failure.
     }
     $responses = $client->sendSync(new RouterOS\Request('/system/resource/print'));
     $data = [];
     foreach ($responses as $response) {
         if ($response->getType() === RouterOS\Response::TYPE_DATA) {
             foreach ($response as $name => $value) {
                 $data[$name] = $value;
             }
         }
     }
     return $data;
     //
 }
 protected function getAddressList($host, $username, $password)
 {
     try {
         $client = new RouterOS\Client($host, $username, $password);
     } catch (Exception $e) {
         die('Unable to connect to the router.');
         //Inspect $e if you want to know details about the failure.
     }
     $responses = $client->sendSync(new RouterOS\Request('/ip/firewall/address-list/print'));
     $data = [];
     $count = 0;
     foreach ($responses as $response) {
         if ($response->getType() === RouterOS\Response::TYPE_DATA) {
             foreach ($response as $name => $value) {
                 $data[$count][$name] = $value;
             }
         }
         $count++;
     }
     return $data;
 }
Exemple #7
0
}
$dnshost = $argv[5];
$list = $argv[4];
$hostname = $argv[1];
$user = $argv[2];
$pass = $argv[3];
$dnsip = array();
$dnsrecord = dns_get_record($dnshost, DNS_A);
foreach ($dnsrecord as $p) {
    $dnsip[] = $p["ip"];
}
$c = new RouterOS\Client($hostname, $user, $pass);
$req = new RouterOS\Request("/ip/firewall/address-list/print");
$query = RouterOS\Query::where("list", $list)->andWhere("comment", $dnshost);
$req->setQuery($query);
$resp = $c->sendSync($req);
$todelete = array();
$toadd = $dnsip;
foreach ($resp as $r) {
    if ($r->getType() === RouterOS\Response::TYPE_DATA) {
        $pos = array_search($r->getProperty("address"), $toadd);
        if ($pos === FALSE) {
            $todelete[] = $r->getProperty(".id");
        } else {
            unset($toadd[$pos]);
        }
    }
}
foreach ($toadd as $ip) {
    echo "Adding {$ip}...\n";
    $req = new RouterOS\Request("/ip/firewall/address-list/add");
<?php

use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
$client = new RouterOS\Client('192.168.0.1', 'admin');
$addRequest = new RouterOS\Request('/ip/arp/add');
$addRequest->setArgument('address', '192.168.0.100');
$addRequest->setArgument('mac-address', '00:00:00:00:00:01');
if ($client->sendSync($addRequest)->getType() !== Response::TYPE_FINAL) {
    die("Error when creating ARP entry for '192.168.0.100'");
}
$addRequest->setArgument('address', '192.168.0.101');
$addRequest->setArgument('mac-address', '00:00:00:00:00:02');
if ($client->sendSync($addRequest)->getType() !== Response::TYPE_FINAL) {
    die("Error when creating ARP entry for '192.168.0.101'");
}
echo 'OK';
<?php

use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
$client = new RouterOS\Client('192.168.0.1', 'admin');
$responses = $client->sendSync(new RouterOS\Request('/ip/arp/print'));
foreach ($responses as $response) {
    if ($response->getType() === Response::TYPE_DATA) {
        echo 'IP: ', $response->getProperty('address'), ' MAC: ', $response->getProperty('mac-address'), "\n";
    }
}
//Example output:
/*
IP: 192.168.0.100 MAC: 00:00:00:00:00:01
IP: 192.168.0.101 MAC: 00:00:00:00:00:02
*/
Exemple #10
0
 public function testFindById()
 {
     $originalResult = $this->client->sendSync(new Request('/queue/simple/print', Query::where('target', HOSTNAME_INVALID . '/32')));
     $this->assertSame($originalResult->getProperty('.id'), $this->util->find($originalResult->getProperty('.id')));
 }