Ejemplo n.º 1
0
 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')));
 }
Ejemplo n.º 2
0
 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');
         }
     }
     //
 }
Ejemplo n.º 3
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.');
     }
 }
Ejemplo n.º 4
0
 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);
     }
 }
Ejemplo n.º 5
0
 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;
     //
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 public function testFilePutAndGetContentsStreamed()
 {
     $data1 = 'test';
     $data2 = 'ok';
     $data1s = fopen('php://temp', 'r+b');
     fwrite($data1s, $data1);
     rewind($data1s);
     $data2s = fopen('php://temp', 'r+b');
     fwrite($data2s, $data2);
     rewind($data2s);
     $this->client->setStreamingResponses(true);
     //New and overwite string
     $putResult1 = $this->util->filePutContents(TEST_FILE_NAME, $data1);
     $getResult1 = $this->util->fileGetContents(TEST_FILE_NAME);
     $putResult2 = $this->util->filePutContents(TEST_FILE_NAME, $data2, true);
     $getResult2 = $this->util->fileGetContents(TEST_FILE_NAME);
     $putResult3 = $this->util->filePutContents(TEST_FILE_NAME, $data1);
     $getResult3 = $this->util->fileGetContents(TEST_FILE_NAME);
     $this->assertTrue($putResult1);
     $this->assertSame($data1, stream_get_contents($getResult1));
     $this->assertTrue($putResult2);
     $this->assertSame($data2, stream_get_contents($getResult2));
     $this->assertFalse($putResult3);
     $this->assertSame($data2, stream_get_contents($getResult3));
     //Removal
     $putResult4 = $this->util->filePutContents(TEST_FILE_NAME, null);
     $getResult4 = $this->util->fileGetContents(TEST_FILE_NAME);
     $putResult5 = $this->util->filePutContents(TEST_FILE_NAME, null);
     $this->assertTrue($putResult4);
     $this->assertFalse($getResult4);
     $this->assertFalse($putResult5);
     //New and overwite stream
     $putResult1 = $this->util->filePutContents(TEST_FILE_NAME, $data1s);
     $getResult1 = $this->util->fileGetContents(TEST_FILE_NAME);
     $putResult2 = $this->util->filePutContents(TEST_FILE_NAME, $data2s, true);
     $getResult2 = $this->util->fileGetContents(TEST_FILE_NAME);
     $putResult3 = $this->util->filePutContents(TEST_FILE_NAME, $data1s);
     $getResult3 = $this->util->fileGetContents(TEST_FILE_NAME);
     $this->assertTrue($putResult1);
     $this->assertSame($data1, stream_get_contents($getResult1));
     $this->assertTrue($putResult2);
     $this->assertSame($data2, stream_get_contents($getResult2));
     $this->assertFalse($putResult3);
     $this->assertSame($data2, stream_get_contents($getResult3));
     //Removal
     $putResult4 = $this->util->filePutContents(TEST_FILE_NAME, null);
     $getResult4 = $this->util->fileGetContents(TEST_FILE_NAME);
     $putResult5 = $this->util->filePutContents(TEST_FILE_NAME, null);
     $this->assertTrue($putResult4);
     $this->assertFalse($getResult4);
     $this->assertFalse($putResult5);
     $this->client->setStreamingResponses(false);
 }
Ejemplo n.º 8
0
<?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);
$client->loop();
Ejemplo n.º 9
0
 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());
 }
Ejemplo n.º 10
0
6. The router has a filter/mangle/nat rule that overrides the settings at
   "/ip service".
   This is a very rare scenario, but if you want to be sure, try to disable all
   rules that may cause such a thing, or (if you can afford it) set up a fresh
   RouterOS in place of the existing one, and see if you can connect to it
   instead. If you still can't connect, such a rule is certainly not the (only)
   reason.

HEREDOC
);
    }
    return;
}
if (null !== $cmd->args['username']) {
    try {
        if (!RouterOS\Client::login($com, $cmd->args['username'], (string) $cmd->args['password'], $comTimeout)) {
            fwrite(STDERR, <<<HEREDOC
Login refused. Possible reasons:

1. No such username.
   Make sure you have spelled it correctly.

2. The user does not have the "api" privilege.
   Check the permissions of the user's group at "/user group".

3. The user is not allowed to access the router from your web server's IP.
   Make sure your web server's IP address is within the subnets the user is
   allowed to log in from. You can check them at the "address" property
   of the user in the "/user" menu.

4. Mistyped password.
Ejemplo n.º 11
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')));
 }
<?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';
Ejemplo n.º 13
0
 public function testInvalidQuerySending()
 {
     $com = new Communicator(\HOSTNAME, PORT);
     Client::login($com, USERNAME, PASSWORD);
     $com->sendWord('/ip/arp/print');
     $com->close();
     try {
         Query::where('address', HOSTNAME_INVALID)->send($com);
         $com->sendWord('');
         $this->fail('The query had to fail.');
     } catch (SocketException $e) {
         $this->assertEquals(SocketException::CODE_QUERY_SEND_FAIL, $e->getCode(), 'Improper exception code.');
     }
 }
Ejemplo n.º 14
0
<?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);
$client->loop();
$responses = $client->extractNewResponses();
foreach ($responses as $response) {
    if ($response->getType() !== Response::TYPE_FINAL) {
        echo "Error with {$response->getTag()}!\n";
    } else {
        echo "OK with {$response->getTag()}!\n";
    }
}
//Example output:
/*
OK with arp1
OK with arp2
*/
<?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
*/
<?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';
Ejemplo n.º 17
0
require "PEAR2_Net_RouterOS-1.0.0b5.phar";
if ($argc < 6) {
    echo "Usage: {$argv[0]} <host> <user> <pass> <address-list> <dns>\n";
    exit;
}
$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]);
        }
    }
<?php

use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';
$client = new RouterOS\Client('192.168.0.1', 'admin');
//Custom function, defined specifically for the example
$responseHandler = function ($response) {
    if ($response->getType() === Response::TYPE_FINAL) {
        echo "{$response->getTag()} is done.\n";
    }
};
$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, $responseHandler);
$addRequest->setArgument('address', '192.168.0.101');
$addRequest->setArgument('mac-address', '00:00:00:00:00:02');
$addRequest->setTag('arp2');
$client->sendAsync($addRequest, $responseHandler);
$client->loop();
//Example output:
/*
arp1 is done.
arp2 is done.
*/