/** * @depends testAdd * * @return void */ public function testDisableAndEnable() { $this->util->setMenu('/queue/simple'); $id = $this->util->add(array('name' => TEST_QUEUE_NAME, 'disabled' => 'no', 'target' => '0.0.0.0/0')); $printRequest = new Request('/queue/simple/print', Query::where('.id', $id)); $this->assertSame('false', $this->client->sendSync($printRequest)->getProperty('disabled')); $this->util->disable($id); $this->assertSame('true', $this->client->sendSync($printRequest)->getProperty('disabled')); $this->util->enable($id); $this->assertSame('false', $this->client->sendSync($printRequest)->getProperty('disabled')); $removeRequest = new Request('/queue/simple/remove'); $removeRequest->setArgument('numbers', $id); $this->client->sendSync($removeRequest); }
public function testCancellingSeparation() { $client = new Client(\HOSTNAME, USERNAME, PASSWORD, PORT, true); $pingRequest = new Request('/ping', null, 'ping'); $pingRequest->setArgument('address', Test\HOSTNAME); $this->object->sendAsync($pingRequest); $client->sendAsync($pingRequest); $client->loop(2); $this->object->loop(2); $this->assertGreaterThan(0, count($client->extractNewResponses('ping'))); $this->assertGreaterThan(0, count($this->object->extractNewResponses('ping'))); unset($client); $this->object->loop(2); $this->assertGreaterThan(0, count($this->object->extractNewResponses('ping'))); }
/** * Executes a RouterOS script. * * Same as the public equivalent, with the addition of allowing you to get * the contents of the script post execution, instead of removing it. * * @param string|resource $source The source of the script, as a string or * stream. If a stream is provided, reading starts from the current * position to the end of the stream, and the pointer stays at the end * after reading is done. * @param array $params An array of parameters to make available * in the script as local variables. * Variable names are array keys, and variable values are array values. * Array values are automatically processed with * {@link static::escapeValue()}. Streams are also supported, and are * processed in chunks, each processed with * {@link static::escapeString()}. Processing starts from the current * position to the end of the stream, and the stream's pointer is left * untouched after the reading is done. * Note that the script's (generated) name is always added as the * variable "_", which will be inadvertently lost if you overwrite it * from here. * @param string $policy Allows you to specify a policy the script * must follow. Has the same format as in terminal. * If left NULL, the script has no restrictions beyond those imposed by * the username. * @param string $name The script is executed after being saved * in "/system script" under a random name (prefixed with the computer's * name), and is removed after execution. To eliminate any possibility * of name clashes, you can specify your own name. * @param bool $get Whether to get the source of the script. * * @return ResponseCollection|string Returns the response collection of the * run, allowing you to inspect errors, if any. * If the script was not added successfully before execution, the * ResponseCollection from the add attempt is going to be returned. * If $get is TRUE, returns the source of the script on success. */ private function _exec($source, array $params = array(), $policy = null, $name = null, $get = false) { $request = new Request('/system/script/add'); if (null === $name) { $name = uniqid(gethostname(), true); } $request->setArgument('name', $name); $request->setArgument('policy', $policy); $params += array('_' => $name); $finalSource = fopen('php://temp', 'r+b'); fwrite($finalSource, '/' . str_replace('/', ' ', substr($this->menu, 1)) . "\n"); static::appendScript($finalSource, $source, $params); fwrite($finalSource, "\n"); rewind($finalSource); $request->setArgument('source', $finalSource); $result = $this->client->sendSync($request); if (0 === count($result->getAllOfType(Response::TYPE_ERROR))) { $request = new Request('/system/script/run'); $request->setArgument('number', $name); $result = $this->client->sendSync($request); if ($get) { $result = $this->client->sendSync(new Request('/system/script/print .proplist="source"', Query::where('name', $name)))->getProperty('source'); } $request = new Request('/system/script/remove'); $request->setArgument('numbers', $name); $this->client->sendSync($request); } return $result; }
$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"); $req->setArgument("address", $ip); $req->setArgument("comment", $dnshost); $req->setArgument("list", $list); $c->sendSync($req); } foreach ($todelete as $id) { echo "Removing {$id}...\n"; $req = new RouterOS\Request("/ip/firewall/address-list/remove"); $req->setArgument("numbers", $id); $c->sendSync($req); } $c->close();
<?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'); $addRequest->setTag('arp1'); $client->sendAsync($addRequest); $addRequest->setArgument('address', '192.168.0.101'); $addRequest->setArgument('mac-address', '00:00:00:00:00:02'); $addRequest->setTag('arp2'); $client->sendAsync($addRequest); foreach ($client->completeRequest('arp1') as $response) { if ($response->getType() === Response::TYPE_ERROR) { echo "Error response for 'arp1'!\n"; } } foreach ($client->completeRequest('arp2') as $response) { if ($response->getType() === Response::TYPE_ERROR) { echo "Error response for 'arp2'!\n"; } } echo 'OK';
public function testTaglessModePassing() { $com1 = new Communicator(\HOSTNAME, PORT, true); Client::login($com1, USERNAME, PASSWORD); $reg1 = new Registry('dummy'); $com2 = new Communicator(\HOSTNAME, PORT, true); $reg2 = new Registry('dummy'); $this->assertNotEquals($reg1->getOwnershipTag(), $reg2->getOwnershipTag()); $pingRequest1 = new Request('/ping address=' . HOSTNAME, null, 'ping'); $pingRequest1->send($com1, $reg1); $response1_1 = new Response($com1, false, null, null, $reg1); $cancelRequest = new Request('/cancel'); $reg1->setTaglessMode(true); $cancelRequest->setArgument('tag', $reg1->getOwnershipTag() . 'ping'); $cancelRequest->send($com1, $reg1); $pingRequest2 = new Request('/ping count=2 address=' . HOSTNAME, null, 'ping'); $pingRequest2->send($com2, $reg2); $response2_1 = new Response($com2, false, null, null, $reg2); $response2_2 = new Response($com2, false, null, null, $reg2); $response2_3 = new Response($com2, false, null, null, $reg2); $reg1->setTaglessMode(false); $com1->close(); $com2->close(); $this->assertEquals(Response::TYPE_DATA, $response2_1->getType()); $this->assertEquals(Response::TYPE_DATA, $response2_2->getType()); $this->assertEquals(Response::TYPE_FINAL, $response2_3->getType()); $response1_2 = new Response($com1, false, null, null, $reg1); $response1_3 = new Response($com1, false, null, null, $reg1); $this->assertEquals(Response::TYPE_DATA, $response1_1->getType()); $this->assertEquals(Response::TYPE_ERROR, $response1_2->getType()); $this->assertEquals(Response::TYPE_FINAL, $response1_3->getType()); $reg1->close(); $this->assertStringStartsWith('-1_', $reg2->getOwnershipTag()); }
$oldid = -1; $same = false; foreach ($resp as $r) { if ($r->getType() === RouterOS\Response::TYPE_DATA) { $oldid = $r->getProperty(".id"); if ($r->getProperty("address") == $ipaddr) { $same = true; } } } if (!$same) { if ($oldid > -1) { echo "Removing old {$ipaddr}...\n"; $req = new RouterOS\Request("/ip/firewall/address-list/remove"); $req->setArgument("numbers", $oldid); $c->sendSync($req); } echo "Adding {$ipaddr}...\n"; $req = new RouterOS\Request("/ip/firewall/address-list/add"); $req->setArgument("address", $ipaddr); $req->setArgument("list", $list); $req->setArgument("timeout", "01:00:00"); $c->sendSync($req); } else { echo "Resetting timeout on {$ipaddr}...\n"; $req = new RouterOS\Request("/ip/firewall/address-list/set"); $req->setArgument(".id", $oldid); $req->setArgument("timeout", "01:00:00"); $c->sendSync($req); } $c->close();
<?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';
/** * Sends a request to RouterOS. * * @param Request $request The request to send. * * @return $this The client object. * @see sendSync() * @see sendAsync() */ protected function send(Request $request) { $request->send($this->com, $this->registry); $this->pendingRequestsCount++; return $this; }
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); } }
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.'); } }