B4J Question Mikrotik Api

walmo

Active Member
Licensed User
Longtime User
Hi
Please i'm looking for a sample of connecting to a Mikrotik router with B4J though a api.
Found this java code.
Please if somebody just can show me in the right direction to do this... THX

Java:
  ApiConnection ret = new ApiConnection("192.168.88.1", 8728);
       if (!ret.isConnected()) {
           ret.start();
           try {
               ret.join();
               if (ret.isConnected()) {
                   ret.login("admin", new char[0]);
               }
           } catch (InterruptedException ex) {
               Logger.getLogger(T3apiView.class.getName()).log(Level.SEVERE, null, ex);
               return null;
           }
       }
       aConn.sendCommand("/ip/address/print");
       DataReceiver dataRec = new DataReceiver(aConn, this);
       dataRec.start();

B4X:
import libAPI.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
  *
  * @author janisk
  */
public class DataReceiver extends Thread {

   private ApiConnection aConn = null;
   T3apiView t3A = null;

   public DataReceiver(ApiConnection aConn, T3apiView t3A) {
       this.aConn = aConn;
       this.t3A = t3A;
   }

   @Override
   public void run() {
       String s = "";
       while (true) {
           try {
               s = aConn.getData();
               if (s != null) {
                   t3A.outputHere(s);
                   if (s.contains("!done")) {
                   }
               }
           } catch (InterruptedException ex) {
               Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
           }
       }
   }
}
 

DonManfred

Expert
Licensed User
Longtime User
Use okhttputils2 to communicate
 
Upvote 0

walmo

Active Member
Licensed User
Longtime User
Hi DonManfred

Thx for reply , i've tried without success , where can i find a sample just to give me a idea how to do this link.
Thank you
 
Upvote 0

Winni

Member
Licensed User
Longtime User
@walmo:

You probably want to connect via ssh or telnet to the router - at least these are the protocols that I use for such tasks with various router brands. I'm a MTCNA, but I haven't worked with Mikrotik routers in a few years - and unlike Cisco or Juniper routers, Mikrotiks are meant to be configured through winbox, not through their rather complicated command line, which makes this a bit more challenging.

You might want to search through this forum to find questions related to ssh and telnet. I've never used B4X for this kind of thing, I've always used Python with the Paramiko library instead - it's very simple to use and the Python library is very mature. Maybe it will make your life easier if you just let B4X launch a Python skript; but since it's not clear what you actually want to do, it's hard to give proper advice.

If you want to write a winbox or a "The Dude" replacement, though, you probably need to look at completely different approach. As far as I know Mikrotik's "The Dude" is written in Lazarus/Free Pascal and if I am not mistaken, the developer from Mikrotik has been posting on the Lazarus/Free Pascal forums a couple of times, maybe you want to search for related posts there and drop the person in Riga a note to get a few useful pointers. ;-)

Good luck!
 
Upvote 0

walmo

Active Member
Licensed User
Longtime User
Thx @Winni
I was looking into telnet connection as i received your message.
Will check if it is easier that http.
Need to manage hotspots and check data from mikrotik
Thank you .
 
Upvote 0
Top