# mikrotik-java
A Java client library implementation for the Mikrotik RouterOS API.
This project provides a Java client to manipulate Mikrotik routers using the remote API. Simple things must be easy to do, and complex things must be possible.
## Versions
**The current stable version is 3.0**
This library uses [semantic versioning](http://semver.org/)
### Changes in version 3.0:
Version 3.0 addresses the problems the API has around TLS encryption. To do this, the way secure connections are implemented is changed, to put the power to do this in the hands of the user. This means that:
* The `connectTLS` API methods were removed.
* A new method, `connect(SocketFactory fact, String host, int port, int timeout)`, was added to allow for better user control over sockets and especially encrypyion.
Further changes include:
* The deprecated `disconnect()` method is removed.
#### Version 2.x
Version 2.2 is the last version 2.x release and will be supported for a limited time.
* 2.2 implemented AutoCloseable on ApiConnection to support Java 7's try-with-resources statement.
* 2.1 added the ability to use connection and command timeouts as.
* 2.0.3 Fixed bug #18 - An empty username in ```login()``` caused the API to hang.
* 2.0.2 Fixed bug #13 - processor thread wasn't being stopped on disconnect(), causing non-exit of application in some cases.
* 2.0.1 fixed parsing of !=, < and > operators in a where clause.
* 2.0.0 changed ResultListener to receive errors and completion notifications. This version is not backwards compatible with version 1.x.
#### Version 1.x
Version 1 is considered *deprecated* and will no longer be supported or patched.
## Getting the API
I recommend using the Maven artifact from Maven Central using this dependency:
```xml
me.legrange
mikrotik
3.0
```
You can also clone or fork the repository, or download the source as a zip or tar.gz from [Releases](https://github.com/GideonLeGrange/mikrotik-java/releases)
## Contributing
I welcome contributions, be it bug fixes or other improvements. If you fix or change something, please submit a pull request. If you want to report a bug, please open an issue. General questions
are also welcome.
# Examples
These examples should illustrate how to use this library. Please note that I assume that the user is proficient in Java and understands the Mikrotik command line syntax.
## Opening a connection
Here is a simple example: Connect to a router and reboot it.
```java
ApiConnection con = ApiConnection.connect("10.0.1.1"); // connect to router
con.login("admin","password"); // log in to router
con.execute("/system/reboot"); // execute a command
con.disconnect(); // disconnect from router
```
To encrypt API traffic (recommended) you need to open a TLS connection to the router. This is done by passing an instance of the `SocketFactory` you wish to use to construct the
TLS socket to the API:
```java
ApiConnection con = ApiConnection.connect(SSLSocketFactory.getDefault(), Config.HOST, ApiConnection.DEFAULT_TLS_PORT, ApiConnection.DEFAULT_CONNECTION_TIMEOUT);
```
Besides allowing the user to specify the socket factory, it also gives full control over the TCP Port and connection timeout.
### Connection timeouts
By default, the API will generate an exception if it cannot connect to the specified router. This can take place immediately (typically if the router returns a 'Connection refused' error), but can also take up to 60 seconds if the router host is firewalled or if there are other network problems. This 60 seconds is the 'default connection timeout' an can be overridded by passing the preferred timeout to the APi as last parameter in a ```connect()``` or ```connectTLS()``` call. Here is the non-TLS example:
```java
ApiConnection con = ApiConnection.connect("10.0.1.1", ApiConnection.DEFAULT_PORT, 2000); // connect to router on the default API port and fail in 2 seconds
```
Connecting using TLS is similar:
```java
ApiConnection con = ApiConnection.connectTLS("10.0.1.1", ApiConnection.DEFAULT_TLS_PORT, 2000); // connect to router on the default TLS API port and fail in 2 seconds
```
Note that ```ApiConnection.DEFAULT_PORT``` and ```ApiConnection.DEFAULT_TLS_PORT``` are provided to allow users who use the default ports to safely use the overloaded timeout method.
### Notes about TLS:
* Currently only anonymous TLS is supported, not certificates.
* There is a compatibility problem between the current versions of RouterOS supporting API over TLS and the Java Cryptography Extension (JCE) in Java 7 and earlier. TLS encryption works in Java 8 and later. For more information, feel free to contact me.
In following examples the connection, login and disconnection code will not be repeated.
## Reading data
A simple example that returns a result: Print all interfaces.
```java
List