Community discussions

MikroTik App
 
dshea
just joined
Topic Author
Posts: 15
Joined: Thu Jan 19, 2012 12:42 am

Need to poll simple value from router with Perl - solved

Fri Feb 03, 2017 12:30 am

I am trying to build a Perl script which will grab the IP address associated with a particular MAC address from the ARP table.

In the CLI, it's easy: just navigate to /ip arp and run the command below (where $t_mac is the colon-delimited, uppercase MAC address)
:put [get [/ip arp find mac-address=$t_mac] address]
and Router OS returns the IP address. But, I want to write a script which grabs the IP address from the router, and I have tried the old Mtik module and SSH, and I'm not getting anywhere. The API doesn't look to be much use, as the 'put' and 'find' commands are not supported (http://wiki.mikrotik.com/wiki/API_comma ... g_commands).

Is there a smart way to do this? I suppose it may be possible to dump (print) the entire ARP table to an array and sort through it, but it seems like a really kludgy way of doing this, given how simple it is in the CLI. Apologies if this has been addressed before, but I couldn't find any forum post which seemed to match my problem.
Last edited by dshea on Wed Feb 15, 2017 5:11 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Need to poll simple value from router with Perl

Fri Feb 03, 2017 3:29 pm

You can use "/ip/arp/print" with a query ("?mac-address=$t_mac"), and get the "address" property from the result.

The beauty of the API (compared to SSH) is precisely that the individual properties can be read with relative ease and reliability (as opposed to SSH, where this is error prone).
 
dshea
just joined
Topic Author
Posts: 15
Joined: Thu Jan 19, 2012 12:42 am

Re: Need to poll simple value from router with Perl

Fri Feb 03, 2017 7:44 pm

That sounds great, but... I have tried to install the API (went to http://search.cpan.org/dist/MikroTik-API/ to get the tar.gz file, followed the instructions in the README), but it will not install.

"make test" always fails with an "Error: Can't locate object method "get_or_add_package_symbol" via package "Package::Stash" at /usr/lib64/perl5/vendor_perl/Class/MOP/Package.pm line 128." Any attempt to use the library results in the same error.

I've made sure I'm running the latest Moose and Package::Stash, but no luck. Running CentOS 6.8, all packages up-to-date according to Yum.

At this point, I'm feeling like I'll have to fins a different way to do this...
 
dshea
just joined
Topic Author
Posts: 15
Joined: Thu Jan 19, 2012 12:42 am

Re: Need to poll simple value from router with Perl

Fri Feb 03, 2017 10:53 pm

OK, so gave up on the Perl approach - successfully got Ruby and the Mtik API interface installed. Successfully connected to a router using the tikcli script...

Feed it the following command:
 /ip/arp/print ?mac-address=00:60:E0:45:CE:AA
It spits out the entire 70 item ARP table, not just the one entry which matches the query (and yes, here it is from the list - I obscured the address, but it was the valid one)

Command (/quit to end): /ip/arp/print ?mac-address=00:60:E0:45:CE:AA
=== COMMAND: /ip/arp/print
<<< '/ip/arp/print' (13)
<<< '?mac-address=00:60:E0:45:CE:AA' (30)
<<< '.tag=4' (6)
<<< END-OF-SENTENCE

[lots of other entries snipped]

>>> '.tag=4' (6)
>>> 'address=x.x.x.x' (19)
>>> '.id=*65843E' (11)
>>> '!re' (3)
>>> 'comment=' (8)
>>> 'mac-address=00:60:E0:45:CE:AA' (29)
>>> 'interface=ether1' (16)
>>> END-OF SENTENCE

[lots of other entries snipped]

>>> '.tag=4' (6)
>>> '!done' (5)
>>> END-OF SENTENCE

So, I might be able to feed this result into some sort of array and loop through the entries to find the data I'm looking for, but it certainly isn't working the way the documentation (http://wiki.mikrotik.com/wiki/API_Ruby_class) indicates it should. Am I missing something here?
 
dshea
just joined
Topic Author
Posts: 15
Joined: Thu Jan 19, 2012 12:42 am

Re: Need to poll simple value from router with Perl

Wed Feb 15, 2017 5:10 pm

Well, persistence finally paid off - I discovered that the Mtik API interface was properly installed, but there had been some other error (probably type mismatch) preventing my earlier efforts from working.

Just to help others in the spot I was in, here's the successful snippet of code:
if (Mtik::login($mtik_host,$mtik_username,$mtik_password)) {
    my %entries = Mtik::get_by_key('/ip/arp/print','mac-address');
    foreach my $entry (keys (%entries)) {
            if ($entry eq $macaddy) {
                $address   = $entries{$entry}{'address'};
                }
        }
    Mtik::logout;
        } else {
        print "Could not log into Mikrotik Router $mtik_host.  Please resolve this and try again.\n";
    }

Who is online

Users browsing this forum: raiser and 71 guests