Martin Gojowsky
and 2 contributors

NAME

MikroTik::API - Client to MikroTik RouterOS API

VERSION

Version 2.0.1

CAUTION: Dependencies change with version 2.0.0. MikroTik::API now relies on Moo instead of Moose. Please be sure, dependencies are met before upgrading.

SYNOPSIS

    use MikroTik::API;

    my $api = MikroTik::API->new({
        host => 'mikrotik.example.org',
        username => 'whoami',
        password => 'SECRET',
        use_ssl => 1,
    });

    my ( $ret_get_identity, @aoh_identity ) = $api->query( '/system/identity/print', {}, {} );
    print "Name of router: $aoh_identity[0]->{name}\n";

    $api->logout();

DESCRIPTION

PUBLIC METHODS

new( \%config )

    my $api = MikroTik::API->new({
        host => 'mikrotik.example.org',
        username => 'whoami',
        password => 'SECRET',
        autoconnect => 1, # optional (set to 0 if you do not want to connect during construction, default: 1)
        use_ssl => 1, # optional (0 for non ssl / 1 for ssl)
        port => 8729, # optonal (needed if you use another port then 8728 for non-ssl or 8729 for ssl)
        debug => 0, # optional (set beween 0 (none) and 5 (most) for debug messages)
        timeout => 3, # optional (timeout after 3 seconds during connect)
        probe_before_talk => 3, # optional (probe connection before each actual command)
        reconnect_after_failed_probe => 1, # optional (reconnect if probe failed)

    });

$api->connect()

Connect happens on construction if you provide host address

    my $api = MikroTik::API->new();

    $api->set_host('mikrotik.example.org');
    $api->set_port(1234);
    $api->set_use_ssl(1);

    $api->connect();

$api->login()

Connect happens on construction if you provide host address, username and password

    my $api = MikroTik::API->new({ host => 'mikrotik.example.org' });

    $api->set_username('whoami');
    $api->set_password('SECRET');

    $api->login();

$api->logout()

    $api->logout();

$api->cmd( $command, \%attributes )

    # Set with no key required
    # /system identity set name=MyNewMikroTik
    my $returnvalue = $api->cmd( '/system/identity/set', { 'name' => 'MyNewMikroTik' } );
    print "Name set\n" if ($returnvalue < 2);

    # Set keyed on the name "local"
    # /interface bridge set local fast-forward=no
    my $returnvalue = $api->cmd( '/interface/bridge/set', { '.id' => 'local', 'fast-forward' => 'no' } );
    print "Bridge fast-forward turned off\n" if ($returnvalue < 2);

    # Set keyed on internal key
    # /interface bridge set *cc fast-forward=no
    my $returnvalue = $api->cmd( '/interface/bridge/set', { '.id' => '*cc', 'fast-forward' => 'no' } );
    print "Bridge fast-forward turned off\n" if ($returnvalue < 2);

    # Reset a value
    # /routing bgp peer set testpeer !keepalive-time
    my $returnvalue = $api->cmd( '/routing/bgp/peer/set', { '.id' => 'testpeer', 'keepalive-time' => undef } );
    print "Reset keepalive-time on testpeer\n" if ($returnvalue < 2);

$api->query( $command, \%attributes, \%conditions )

    # Get all interfaces of type ether
    my ( $ret_interface_print, @interfaces ) = $api->query('/interface/print', { '.proplist' => '.id,name' }, { type => 'ether' } );
    foreach my $interface ( @interfaces ) {
        print "$interface->{name}\n";
    }

    # get all default routes that don't have the dynamic attribute
    my ( $ret_route_print, @routes ) = $api->query('/ip/route', { '.proplist' => '.id,dst-address' }, { 'dst-address' => '0.0.0.0/0', 'dynamic'=>undef } );
    foreach my $route ( @routes ) {
        print "$route->{'dst-address'}\n";
    }

    # get all default routes that don't have the dynamic attribute (alternate using array ref)
    my ( $ret_route_print, @routes ) = $api->query('/ip/route', { '.proplist' => '.id,dst-address' }, [ 'dst-address=0.0.0.0/0', '-dynamic' ] );
    foreach my $route ( @routes ) {
        print "$route->{'dst-address'}\n";
    }

    # get all default routes along with those with the dynamic attribute (note 'or' operator as last arg)
    my ( $ret_route_print, @routes ) = $api->query('/ip/route', { '.proplist' => '.id,dst-address' }, [ 'dst-address=0.0.0.0/0', 'dynamic', '#|' ] );
    foreach my $route ( @routes ) {
        print "$route->{'dst-address'}\n";
    }

$api->get_by_key( $command, $keycolumn )

    my %interface = $api->get_by_key('/interface/ethernet/print', 'name' );
    print "$interface{'ether1'}->{running}\n";

ACCESSORS

$api->get_host(), $api->set_host( $hostname )

$api->get_port(), $api->set_port( $portnumber )

$api->get_username(), $api->set_username( $username )

$api->get_password(), $api->set_password( $password )

$api->get_use_ssl(), $api->set_use_ssl( $zero_or_one )

$api->get_ssl_verify(), $api->set_ssl_verify( $zero_or_one )

$api->get_new_auth_method(), $api->set_new_auth_method( $zero_or_one )

DEPRECATED: does not have any effect any longer. Login looks up wether new method is possible and falls back to old method. This parameter will be removed in future. Auth method changed in RouterOS v6.43+ (https://wiki.mikrotik.com/wiki/Manual:API#Initial_login) and reduces login by one call but sends password in plaintext.

$api->get_autoconnect(), $api->set_autoconnect( $zero_or_one )

$api->get_socket(), $api->set_socket( $io_socket )

If you need to use an existing socket for the API connection.

    my $socket = IO::Socket::INET->new();
    $api->set_socket( $socket );

$api->get_debug(), $api->set_debug( $int )

    $api->set_debug(0); # no debug
    $api->set_debug(5); # verbose debug to STDOUT

$api->get_timeout(), $api->set_timeout( $seconds )

Abort connect after $seconds of no reply from MikroTik. This _will not_ affect lost connections. Use probe_before_talk for this.

$api->get_probe_before_talk(), $api->set_probe_before_talk( $seconds )

Use this attribute to enable a test command with timeout to ensure that the connection is still alive before sending the actual command. This is very useful for long lasting connections that may get disconnected while idling. A broken connection will not be recognized otherwise, because the socket still exists and the command will last forever. The advantage over a common timeout for all commands is that long lasting commands are still possible. Set this to 0 if you use many consequent commands and reenable it after completion.

    $api->set_probe_before_talk(0); # no probing of connection before sending command and read reply
    $api->set_probe_before_talk(5); # a simple command will be sent and after 5 seconds of no reply, the connection is assumed as broken

$api->get_reconnect_after_failed_probe(), $api->set_reconnect_after_failed_probe( $zero_or_one )

If connection is recognized as broken then either reconnect or die otherwise.

SEMI-PUBLIC METHODS

can be useful for advanced users, but too complex for daily use

$api->talk( \@sentence )

$api->raw_talk( \@sentence )

ABOUT

Contributors

Object-Orientated Rebuild of prior contributions, based on:

  • initial release from cheesegrits in MikroTik forum: http://forum.mikrotik.com/viewtopic.php?p=108530#p108530

  • added timeout parameter and fixes by elcamlost: https://github.com/elcamlost/mikrotik-perl-api/commit/10e5da1fd0ccb4a249ed3047c1d22c97251f666e

  • SSL support by akschu: https://github.com/akschu/MikroTikPerl/commit/9b689a7d7511a1639ffa2118c8e549b5cec1290d

  • upgrade to v2.0.0 by Steffen Winkler

Design decisions

  • Use of Moo for OO

  • higher compilation time of Moo based lib negligible because of slow I/O operations

  • Change from Moose to Moo with version 2.0.0 because of XS dependencies of Moose

AUTHOR

Martin Gojowsky, martin at gojowsky.de

BUGS

Please report any bugs or feature requests to bug-mikrotik-api at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MikroTik-API. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

Known issues

  • Quite high compile time because of using Moo. Use of a persistent running framework recommended.

TODO

  • Add a parameter talk_timeout as an alternative for probe_before_talk that enables an actual timeout for each command.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MikroTik::API

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2015 Martin Gojowsky.

This program is distributed under the MIT (X11) License: http://www.opensource.org/licenses/mit-license.php

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.