retrieving information via telnet or php api

Hi.
I´m looking for the way to access to routerboard and retrieve some data like signal,ccq, something diferent like snmp etc…

I start try with access by two diferent way without success:

1) The first is this perl script :

    #!/usr/bin/perl
    ###############################################################################
    use diagnostics;
    use Net::Telnet;
    use strict;
    use warnings;
    ##############################################################################


    my $telnet = new Net::Telnet( Timeout=>10, Errmode=>'die');
    $telnet->open('192.168.2.5');
    print "Esperando login\n";
    $telnet->waitfor('/Login: $/i');
    $telnet->print('admin');
    print "Esperando pass\n";
    $telnet->waitfor('/Password: $/i');
    $telnet->print('mipass');
    print "Esperando prompt\n";
    $telnet->waitfor('/] >/i');
    $telnet->print('interface wireless print');
    my $output = $telnet->waitfor('/\$ $/i');
    print $output;

But the system not recognize the prompt regular expression and script wait until time-out error:

Uncaught exception from user code:
pattern match timed-out at ./pruebamikro.pm line 29
at /usr/share/perl5/Net/Telnet.pm line 2036
Net::Telnet::_croak(‘Net::Telnet=GLOB(0x9b129b0)’, ‘pattern match timed-out’) called at /usr/share/perl5/Net/Telnet.pm line 539

I take the prompt regular expression from : http://arulgobi.blogspot.com/2010/04/mikrotik-backup-through-perl-script.html

2) I try access by PHP API : http://wiki.mikrotik.com/wiki/API_PHP_class

With simple code like this:

    <?php

    require('routeros_api.class.php');
    $API = new routeros_api();
    $API->debug = true;

    if ($API->connect('192.168.2.5', 'admin', 'mipass')) {

       $API->write('/interface print');

       $READ = $API->read(false);
       $ARRAY = $API->parse_response($READ);
       print_r($ARRAY);
       $API->disconnect();
    }
    ?>

But return me this error:

Connection attempt #1 to 192.168.2.5:8291…
<<< [6] /login
PHP Notice: Undefined variable: _ in /home/p/Escritorio/enphp/routeros_api.class.php on line 304
PHP Notice: Undefined offset: 0 in /home/p/Escritorio/enphp/routeros_api.class.php on line 97
Connection attempt #2 to 192.168.2.5:8291…

Some help? Any experiences??

Thanks!

Trying to parse output from Telnet is probably a bad idea…

As for the API, one typically needs to either read the API page in full to get it (“/” instead of spaces, type checking, etc.)… and/or use a more fancy client, such as the ones from the “Elsewhere” section on the API page (i.e. PEAR2_Net_RouterOS or mikrotik4net).

With that PHP client, you can for example print some interface info with something like:

<?php
namespace PEAR2\Net\RouterOS;
require_once 'PEAR2/Net/RouterOS/Autoload.php';

try {
    $client = new Client('192.168.2.5', 'admin', 'mipass');

    $printRequest = new Request('/interface print');

    foreach ($client->sendSync($printRequest) as $response) {
        switch ($response->getType()) {
        case Response::TYPE_DATA:
            echo 'The MTU of ', $response->getArgument('name'), ' is ', $response->getArgument('mtu'), "\n";
            break;
        case Response::TYPE_ERROR:
            echo "\n\nThere was the following error response:\n";
            foreach ($response->getAllArguments() as $name => $value) {
                echo "{$name}: {$value}\n";
            }
            echo "\n";
            break;
        default:
            echo 'That is all!';
        }
    }
} catch (Exception $e) {
    echo $e;
}
?>

Hi
Thank you very much boen_robot, I try with pear2_net_routerOS and I connect without problems and send commands like /interface wireless registration-table print.

But, when I send the follow request:

   
$printRequest = new Request('/interface wireless registration-table print stats');

Return me

There was the following error response:
message: no such command

I´m looking for the client´s ccq.

Thanks :smiley:

The client doesn’t really “know” the commands available in RouterOS, so it just passes it along as one. In other words, you’re calling a command “stats” as opposed to “print” command with an argument “stats”. There is no command “stats”, hence the error message.

You need to explicitly mark the moment where the command ends by setting a value to the first argument, like:

$printRequest = new Request('/interface wireless registration-table print stats=""'); 

after which you can continue without it, e.g.

$printRequest = new Request('/interface wireless registration-table print stats="" detail');  

There are also a few other caveats in what you can do at the Request constructor. See this page from the docs for details.

i am sorry, but:
http://wiki.mikrotik.com/wiki/API
http://wiki.mikrotik.com/wiki/API_command_notes

read and check out example link.

Ok, thanks for your help guys, I will read documents.

Thanks again