perl API client

cheesegrits did you tested len>=0x200000?

I think

print $sock chr(($len >> 8) & 0xFF);
print $sock chr(($len >> 8) & 0xFF);
print $sock chr(($len >> 8) & 0xFF);


will print one value. Maybe you mean

print $sock chr(($len >> 24) & 0xFF);
print $sock chr(($len >> 16) & 0xFF);
print $sock chr(($len >> 8) & 0xFF);

yeah?

Then I read wiki.mikrotik.com/wiki/API:

len >= 0x10000000
0xF0 and len as four bytes

so your code is

 elsif ($len < 0x10000000)
    {
        $len |= 0xE0000000;

0xE0000000 != 0xF000000000

and better elsif ($len < 0x10000000) → else

The formula is seems simple. My code:

$bytes=$len < 0x80? 1 : $len < 0x4000? 2 : $len < 0x200000? 3 : $len < 0x10000000? 4 : 5;
$len|=(0x0F >> (5-$bytes)) << ($bytes*8-$bytes+1);
while ($bytes--)
   {
    $str.=chr($len & 0xFF);
    $len>>=8;
   }

$str - rezult

I mean

elsif ($len < 0x10000000)
...
elsif ($len < 0x10000000)

2 Identical conditions

sub read_len
{
 my ($len,$c,$bytes);
 $sock->recv($c,1);
 $c=ord($c);
 $bytes=$c<0x80? 0 : $c<0xC0? 1 : $c<0xE0? 2 : $c<0xF0? 3 : 4;
 $len=$c & (0xFF >> $bytes);
 $bytes or return $len;
 while ($bytes--)
 { 
    $sock->recv($c,1);
    $len=($len << 8) + ord($c);
 }
 return $len;
}
if ($word =~ /!done/)
{
    $retval = 1;
}

if ($word =~ /^!done/)
{
    $retval = 1;
}

imagine a reply like that:

=name=system!done

in API, you can use shortened value names, like in CLI; e.g.

/ip/route/print
=n=*1
=val=routing-mark

instead of

/ip/route/print
=numbers=*1
=value-name=routing-mark

isn’t it a bit dangerous? maybe it should be forbidden, to avoid possible future problems? API is not CLI for humans…

users have to use full command/attribute names when writing API command. Shortened names will not be allowed in 3.25+ versions

What’s new in 3.25:

*) api - do not accept truncated property names;

coool =)

yes, thank you very much for the report :slight_smile: will you come to MUM in USA?

unfortunately, no - it’s too expensive for me =(
I’ll wait for the next MUM in Europe, CZ MUM was the best ))

during your MUM in USA I’ll be looking for such issues with API here, in Minsk :smiley:

When are you guys going to have a MUM in Japan?

This perl api need to be rewritten.
It has also errors in general getting answer where !done= !trap= !fatal,
→ sub talk and and sub read_sentence

I would like to thanks to the author.
All my networks works managed using API Perl + PHP.
api.perl gets statistics, signals, macs
ap.php manage users,queues


sub read_sentence {
    my ($word);
    my ($i) = 0;
    my (@reply);
    my($retval) = 0;
    my($done) = 0;
    # tu trzeba dopisać obsluga trap
    while ($word = &read_word())
    {
            if ($word =~ /^!done/)
            {
                $retval = 1;
                $done=1;
            }
            elsif ($word =~ /^!trap/)
            {
                $retval = 2;
            }
            elsif ($word =~ /^!fatal/)
            {
                $retval = 3;
            }
        $reply[$i++] = $word;
        if ($debug > 2)
        {
            print STDERR "MT: $word\n"
        }
    }
    return ($done,$retval,@reply);
}

######## PUBLIC FUNCTIONS ############

sub talk
{
    #my(@sentence) = shift;
    my($sentence_ref) = shift;
    my(@sentence) = @$sentence_ref;
    &write_sentence(\@sentence);
    my(@reply);
    my(@attrs);
    my($i) = 0;
    my($retval) = 0;
    my($done) = 0;
    while (($done,$retval,@reply) = &read_sentence())
    {
        foreach my $line (@reply)
        {
            if ($line =~ /^=(\S+)=(.*)/)
            {
                $attrs[$i]{$1} = $2;
            }
        }
        if ($retval > 0 && $done>0)
        {
            last;
        }
        
        $i++;
    }
    return ($retval, @attrs);
}

HELP … i update all RB from 3.20 to 3.28 and some scripts in perl stops
settings parameters - OK
/interface/print - OK
/interface/wireless/accesslist/print - OK

but

/interface/wireless/registration-table/print - CRASH

what is wrong ??

help me …
Krzys

How many client stations is connected to this mikrotik?
There is a bug which losts data when you try to get more then 1KB data.
Try to change first doanloaded perl api and apply all canges on forum site in this thread.

if i remember correctly then first releases of Perl API lacked support for long responses. Do as ema said

hmm .. many connected stations … sometimes show 6 stations (maybe it is 1kB)

I try changed perl api but does’t work fine - sometimes show 6 stations - sometimes crash

Maybe check mine perl API packege: http://zwierzu.zepsul.net/Mtik.pm.txt

sample code:

#!/usr/bin/perl

use strict;
use warnings;
use Mtik;
use Data::Dumper;


       my $mt = Mtik->new( host => 'host', user => 'admin', pass => 'pass');

        if ($mt->login()) {
            my $regtable_api = $mt->get_by_key('/interface/wireless/registration-table/print','=stats', 'mac-address');
            print Dumper $regtable_api;
        }

Works for me..

localhost sbin # perl dupa.pl

$VAR1 = {
          '00:15:6D:D0:0F:F5' => {
                                   'rx-ccq' => '17',
                                   'hw-frame-bytes' => '42100172,7240063',
                                   'packets' => '49461,38274',
                                   'nstreme' => 'false',
                                   'ack-timeout' => '28',
                                   'uptime' => '08:03:54',
                                   'interface' => 'wlan3',
                                   'last-activity' => '00:00:03.350',
                                   'frame-bytes' => '40615897,6306238',
                                   'mac-address' => '00:15:6D:D0:0F:F5',
                                   'framing-mode' => 'none',
                                   '802.1x-port-enabled' => 'true',
                                   'routeros-version' => '3.2',
                                   'ap' => 'false',
                                   'bytes' => '40910305,6535882',
                                   'signal-to-noise' => '34',
                                   'tx-ccq' => '97',
                                   'radio-name' => 'Motozbyt',
                                   'frames' => '49461,38274',
                                   'hw-frames' => '49738,38665',
                                   'tx-frames-timed-out' => '0',
                                   'compression' => 'false',
                                   'wmm-enabled' => 'false',
                                   'p-throughput' => '18146',
                                   'tx-signal-strength' => '-67',
                                   'wds' => 'false',
                                   'signal-strength' => '-70dBm@6Mbps',
                                   'comment' => '',
                                   '.id' => '*4',
                                   'rx-rate' => '12Mbps',
                                   'strength-at-rates' => '-70dBm@6Mbps 1s350ms,-70dBm@9Mbps 18s380ms,-71dBm@12Mbps 3s350ms,-71dBm@18Mbps 3m39s750ms,-72dBm@24Mbps 3m43s620ms',
                                   'tx-rate' => '24Mbps'
                                 },

Hi I’ve been playing around allot with the perl API client, and have been testing a few commands on the TTY wrapper, however I’m having trouble trying to enable a defined queue in the simple queues table

/queue/simple-table/print
#lists queues
>>> !re
>>> =.id=*2
>>> =name=limit-vlan3-tc
>>> =target-addresses=192.168.192.0/22
>>> =dst-address=0.0.0.0/0
>>> =interface=vlan3-192.168.192.0/22 VM TC
>>> =parent=none
>>> =direction=both
>>> =priority=8
>>> =queue=default-small/default-small
>>> =limit-at=0/0
>>> =max-limit=2M/2M
>>> =burst-limit=0/0
>>> =burst-threshold=0/0
>>> =burst-time=0s/0s
>>> =total-queue=default-small
>>> =bytes=0/0
>>> =total-bytes=0
>>> =packets=0/0
>>> =total-packets=0
>>> =dropped=0/0
>>> =total-dropped=0
>>> =rate=0/0
>>> =total-rate=0
>>> =packet-rate=0/0
>>> =total-packet-rate=0
>>> =queued-packets=0/0
>>> =total-queued-packets=0
>>> =queued-bytes=0/0
>>> =total-queued-bytes=0
>>> =lends=0/0
>>> =total-lends=0
>>> =borrows=0/0
>>> =total-borrows=0
>>> =pcq-queues=0/0
>>> =total-pcq-queues=0
>>> =disabled=true
>>> =invalid=false
>>> =dynamic=false
>>> !done

When I specify the =.id=*2 and then =disable=false it returns

!done
<<<
!trap
=message=no such command prefix

And i’m unsure if i’m specifying this right
Thanks in advance

/queue/simple**-table** ?..

what exact command do you try to set ‘disabled’ to false?..

I have a list of simple queue’s that I wish to be able to disable and enable from a perl script using the API,
/queue/simple-table/print will list the table of simple scripts, but my question is how do I enable the predefined scripts?