A Java client for Mikrotik API based on Apache MINA (NIO sockets)
This version is the first public available library, so it most likely has bugs.
This library depends on Apache MINA. Tested version is 2.0.9.
The easy way to start using is:
- Download the latest version of library and put in your lib folder
- Download the Apache MINA library and put in your lib folder
- Use it
Here are some examples to use the library.
Connecting and getting results
MikrotikConnection conn = new MikrotikConnection();
try {
MikrotikSession session = con.connect("<ip of router>", 8728);
try {
session.login("<user>", "<pass>");
List<Map<String, String>> res = session.execute(new Command("/system/routerboard/print"));
for(Map<String, String> item : res) {
System.out.println(item.getKey() + "=" + item.getValue());
}
} finally {
session.close();
}
} finally {
conn.close();
}
Connection through TLS
Certificate cert = ...;
KeyPair keyPair = ...;
MikrotikConnection conn = new MikrotikConnection();
try {
MikrotikSession session = con.connect("<ip of router>", 8729, certificate, keyPair.private);
try {
session.login("<user>", "<pass>");
List<Map<String, String>> res = session.execute(new Command("/system/routerboard/print"));
for(Map<String, String> item : res) {
System.out.println(item.getKey() + "=" + item.getValue());
}
} finally {
session.close();
}
} finally {
conn.close();
}
The command accepts the path, some attributes and queries.
Command cmd = new Command("/system/reboot"); //simple command
Map<String, String> attrs = new HashMap<String, String>();
attrs.put("numbers", "*0")
Command cmd = new Command("/interface/print", attrs); //simple command with attributes
Command cmd = new Command("/interface/print", new CommandQueryExpression("name", CommandQueryOperation.EQUALS, "ether1")); //simple command with query
Map<String, String> attrs = new HashMap<String, String>();
attrs.put("numbers", "*0")
Command cmd = new Command("/interface/print", attrs, new CommandQueryExpression("name", CommandQueryOperation.EQUALS, "ether1"));
Found any bug, or want to improve the code (or this readme)? Any pull request is welcome. Want only to report? Open an issue. Thanks!
This library is released under the Apache 2.0 licence. See the License file.