API login/logout

I am currently writing a API program for my Blackberry. I see this in most posts about API commands here:
Connecting…
command sent
response received
Disconnecting…

You don’t log in, send one command, wait for one response, then log out, do you? I logout only when I am finished with all commands. But I use a thread to open, monitor and close the connection.

so, what’s your question?.. :slight_smile:

yep, normal way is “connect, login, do what you want, disconnect”. but in examples on the forum there’s frequently just 1-2 commands ))

Thanks Chupaka. That answered it. I like my way better. I can view and edit many sections of the setup with one login.

sure. that’s what command tagging was implemented for - you can even execute simultaneous monitoring commands via one connection

Hey Chupaka! I hear you wrote an API script for this OS. Any way you could post the link to that script for me? Doesn’t matter what language it is in. I speak many computer languages. I would really like to see it! Maybe some inspiration will come of it. Thanks for all! :smiley:

all I have is http://forum.mikrotik.com/t/another-routeros-api-delphi-client/28181/1 =)

Correct me if I’m wrong, but you cannot close an API session in RouterOS? At least nothing is mentioned in API documentation. I’m mostly using PHP implementation and it only closes socket that has been opened, which in fact doesn’t kill the entry in active session list.

Mikrotik responded and said that this bug of sessions not timing out is fixed in ROS v5. Unfortunately I don’t think I want to move to v5 for now and I use API queries alot. The problem is that when ROS issues about 100 challenges it responds with “cannot get challenge” error and you can’t connect to ROS via API anymore. Reboot being the only way to get it working again is totally out of question for me, so maybe some bright minds could suggest something or at least put worries to an end by telling there’s nothing more to do about it?

I really do like working with API, it’s very convenient and simple. Since everything is already in place and working as it should (except for that challenge thingy), I don’t want to rewrite everything using PHP telnet class. If somehow I could overcome that session overfilling issue, I could get myself a nice vacation :wink:

API closes login when you close connection gracefully.

But you are able to issue /quit command to close login, but note that to re-connect again you will have to close connection and create that again to get challenge, so there is no point of using /quit since you will have to close connection anyway.

Use case for API is not to monitor, but to do other things as for monitoring you can use SNMP, for example the Dude

Ok, I shall try /quit’ing after acquiring data or making changes, I don’t think this would cause troubles for me as long as that challenge issuing thing doesn’t apply. Currently I’m using the PHP API script provided in Mikrotik Wiki. I haven’t modified it at all, and it’s application doesn’t differ much from the example of use provided in that same Wiki page. I don’t want to seem annoying, but how do you imagine graceful connection close?

I use API for controlling firewall adress-list entries. The custom web management system is used to enable or disable internet access or create redirects to custom pages for certain customers. I see of no other way of achieving that, with an exception of telnet. My clients often get friendly remainders that they haven’t paid yet, and this means they need to press “Continue browsing” button which in turn eventually calls for API script to remove entries from ROS firewall addr-list.

  1. The user initiates by telling the TCP to CLOSE the
  2. The remote TCP initiates by sending a FIN control signal
  3. Both users CLOSE simultaneously


    when socket is closed attached API sessions is closed too. (at least it should by design)

I don’t know how things are handled past PHP API implementation and I don’t think I am qualified enough to dig any deeper. The connection is closed simply by closing the socket with fclose() that has previously been opened with fsockopen(). What else could you do? People are reporting that other implementations doesn’t work correctly either, so what needs fixing here?

Btw, /quit doesn’t end session in Active session list either.

As mentioned, this is a bug. I don’t think there’s anything that will fix it other then Mikrotik fixing it in v5. I think v4 will not be fixed.
Also see here for more information: http://forum.mikrotik.com/t/php-api-connection/33417/1

Please define gracefully.

Use case for API is not to monitor, but to do other things as for monitoring you can use SNMP, for example the Dude

What is use case for API?

Monitoring with SNMP.. yes it is doable, but it takes ages to complete to scan one busy MT system.
Yes - you implemented snmp bulk pdu (cool ), but only In 5.x which is rc.

Is it so difficult to backport working API from 3.30 or 5.x to 4.x branch, and snmp bulk pdu from 5.x to 4.x ?
Or maybe you have no interest in doing that.

I define gracefully as closing the connection when I am finished. I use Java and a thread to do the communication with the router. That way if the thread stalls, I can recover it with the main program. The main program controls the thread with vectors holding the commands and boolean control bits as “signal lines” between the thread and the program, like a Unix fork and pipe.

My Blackberry does very well at handling the connection. My router shows one login and one logout, no matter how long I stay connected or how many commands I use. The API is easier for a newbie than CLI.

23:19:50.802868 IP 10.1.1.21.8728 > devel.54230: FP 375:412(37) ack 105 win 2896 <nop,nop,timestamp 579513 8197338>
23:19:50.802938 IP devel.54230 > 10.1.1.21.8728: . ack 413 win 108 <nop,nop,timestamp 8197340 579513>
23:19:56.160016 IP devel.54230 > 10.1.1.21.8728: F 105:105(0) ack 413 win 108 <nop,nop,timestamp 8198679 579513>
23:19:56.161746 IP 10.1.1.21.8728 > devel.4230: . ack 106 win 2896 <nop,nop,timestamp 580049 8198679>

It this graceful enough ? (10.1.1.21 is MT after issuing /quit command)

What versions of ROS do you use ?

How about this for graceful? From my main program:

Main.Connect=false;

The thread handles the rest.

I use V3.30 and V4.14

ADD: and when the thread has closed the connection, it does this:

Main.Connected=false;

Could you paste the tcpdump from your app while closing connection ?

I am not certain I can do a tcpdump from my Blackberry. But why would I need to? The API responds perfectly, and the router shows the login, the correct changes, and a logout. I can log in again right away, do more changes, and logout without exiting the Java program. In the main program:

Main.Connect=true;

Wait for thread to change Main.Connected=true.

I’m using python API client - slightly modified version of python client example ftom wiki: http://wiki.mikrotik.com/wiki/Manual:API

Mine is a Java version of that script in a way. The “while1” loop is the thread code. But I don’t see where that code closes the connection. ??