Compare commits
15 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
a3d9be54f7 | |
|
|
8d056a7cea | |
|
|
60fe180b81 | |
|
|
0243318870 | |
|
|
4939c65f57 | |
|
|
ec5f665081 | |
|
|
e3cf6a933a | |
|
|
ed0468dbf9 | |
|
|
bfbe50732d | |
|
|
75a140f79f | |
|
|
3424482aab | |
|
|
7a629d3b85 | |
|
|
603f34ca6e | |
|
|
0f312c2232 | |
|
|
5bc5263aaf |
|
|
@ -0,0 +1,24 @@
|
||||||
|
# This workflow will build a Java project with Maven
|
||||||
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||||
|
|
||||||
|
name: Java CI with Maven
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Set up JDK 1.8
|
||||||
|
uses: actions/setup-java@v1
|
||||||
|
with:
|
||||||
|
java-version: 1.8
|
||||||
|
- name: Build with Maven
|
||||||
|
run: mvn -B package --file pom.xml
|
||||||
|
|
@ -6,11 +6,11 @@ This project provides a Java client to manipulate Mikrotik routers using the rem
|
||||||
|
|
||||||
## Versions
|
## Versions
|
||||||
|
|
||||||
[](https://travis-ci.org/GideonLeGrange/mikrotik-java)
|

|
||||||
|
|
||||||
**The current stable version is 3.0.7**
|
**The current stable version is 3.0.8**
|
||||||
|
|
||||||
Version 3.0.7 fixes nested expressions when constructing complex queries using `and` and `or` expressions. See [#72](https://github.com/GideonLeGrange/mikrotik-java/issues/72) for more information
|
Version 3.0.8 fixes a null pointer error
|
||||||
|
|
||||||
### Changes in version 3.0:
|
### Changes in version 3.0:
|
||||||
|
|
||||||
|
|
@ -35,7 +35,7 @@ Maven users can use the artifact from Maven Central with this dependency:
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>me.legrange</groupId>
|
<groupId>me.legrange</groupId>
|
||||||
<artifactId>mikrotik</artifactId>
|
<artifactId>mikrotik</artifactId>
|
||||||
<version>3.0.7</version>
|
<version>3.0.8</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
10
pom.xml
10
pom.xml
|
|
@ -9,7 +9,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>me.legrange</groupId>
|
<groupId>me.legrange</groupId>
|
||||||
<artifactId>mikrotik</artifactId>
|
<artifactId>mikrotik</artifactId>
|
||||||
<version>3.0.7</version>
|
<version>3.0.8.1</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<name>Mikrotik API Java Client Library</name>
|
<name>Mikrotik API Java Client Library</name>
|
||||||
<url>https://github.com/GideonLeGrange/mikrotik-java</url>
|
<url>https://github.com/GideonLeGrange/mikrotik-java</url>
|
||||||
|
|
@ -33,8 +33,8 @@
|
||||||
<version>2.3.2</version>
|
<version>2.3.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<showDeprecation>true</showDeprecation>
|
<showDeprecation>true</showDeprecation>
|
||||||
<source>1.7</source>
|
<source>8</source>
|
||||||
<target>1.7</target>
|
<target>8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
@ -73,7 +73,7 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
<version>2.9.1</version>
|
<version>3.7.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludePackageNames>example;*.impl</excludePackageNames>
|
<excludePackageNames>example;*.impl</excludePackageNames>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
@ -161,7 +161,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ package examples;
|
||||||
*/
|
*/
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
public static final String HOST = "192.168.1.25";
|
public static final String HOST = "ce2.ter.cpt.adept.za.net";
|
||||||
public static final String USERNAME = "admin";
|
public static final String USERNAME = "adept";
|
||||||
public static final String PASSWORD = "";
|
public static final String PASSWORD = "34ffp9";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
package examples;
|
||||||
|
|
||||||
|
import me.legrange.mikrotik.MikrotikApiException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example 1: A very simple command: Reboot the remote router
|
||||||
|
* @author gideon
|
||||||
|
*/
|
||||||
|
public class ScriptCommand extends Example {
|
||||||
|
|
||||||
|
public static void main(String...args) throws Exception {
|
||||||
|
ScriptCommand ex = new ScriptCommand();
|
||||||
|
ex.connect();
|
||||||
|
ex.test();
|
||||||
|
ex.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void test() throws MikrotikApiException {
|
||||||
|
con.execute("/ip/hotspot/user/profile/add name=\"cahyo-cek-2\" idle-timeout=none keepalive-timeout=2m status-autorefresh=1m shared-users=1 add-mac-cookie=true mac-cookie-timeout=3d parent-queue=none rate-limit=512k/512k on-login=:put(\",re,3000,12d,3000,,Enable,\")");
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/*
|
||||||
|
/ip/hotspot/user/profile/add name="cahyo-cek-2" idle-timeout=none keepalive-timeout=2m status-autorefresh=1m shared-users=1 add-mac-cookie=true mac-cookie-timeout=3d parent-queue=none rate-limit=512k/512k on-login=:put (",re,3000,12d,3000,,Enable,");
|
||||||
|
/system scheduler add name="usercahyo-cek-2-$user" interval=12:00:00 on-even="/ip hotspot active remove [find user=\"$user\"] ; /ip hotspot user set \"$user\" disable=yes ; /system scheduler remove [find name=usercahyo-cek-2-$user]"
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -14,6 +14,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
import me.legrange.mikrotik.ApiConnection;
|
import me.legrange.mikrotik.ApiConnection;
|
||||||
import me.legrange.mikrotik.ApiConnectionException;
|
import me.legrange.mikrotik.ApiConnectionException;
|
||||||
|
|
@ -162,8 +163,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized String nextTag() {
|
private synchronized String nextTag() {
|
||||||
_tag++;
|
return Integer.toHexString(_tag.incrementAndGet());
|
||||||
return Integer.toHexString(_tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Socket sock = null;
|
private Socket sock = null;
|
||||||
|
|
@ -173,7 +173,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private Reader reader;
|
private Reader reader;
|
||||||
private Processor processor;
|
private Processor processor;
|
||||||
private final Map<String, ResultListener> listeners;
|
private final Map<String, ResultListener> listeners;
|
||||||
private Integer _tag = 0;
|
private final AtomicInteger _tag = new AtomicInteger(0);
|
||||||
private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT;
|
private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -186,7 +186,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String take() throws ApiConnectionException, ApiDataException {
|
private String take() throws ApiConnectionException, ApiDataException {
|
||||||
Object val = null;
|
Object val;
|
||||||
try {
|
try {
|
||||||
val = queue.take();
|
val = queue.take();
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
|
|
@ -223,7 +223,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private void put(Object data) {
|
private void put(Object data) {
|
||||||
try {
|
try {
|
||||||
queue.put(data);
|
queue.put(data);
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -255,20 +255,24 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
} catch (MikrotikApiException ex) {
|
} catch (MikrotikApiException ex) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ResultListener l = listeners.get(res.getTag());
|
if (res.getTag() != null) {
|
||||||
if (l != null) {
|
ResultListener l = listeners.get(res.getTag());
|
||||||
if (res instanceof Result) {
|
if (l != null) {
|
||||||
l.receive((Result) res);
|
if (res instanceof Result) {
|
||||||
} else if (res instanceof Done) {
|
l.receive((Result) res);
|
||||||
if (l instanceof SyncListener) {
|
} else if (res instanceof Done) {
|
||||||
((SyncListener) l).completed((Done) res);
|
if (l instanceof SyncListener) {
|
||||||
} else {
|
((SyncListener) l).completed((Done) res);
|
||||||
l.completed();
|
} else {
|
||||||
|
l.completed();
|
||||||
|
}
|
||||||
|
listeners.remove(res.getTag());
|
||||||
|
} else if (res instanceof Error) {
|
||||||
|
l.error(new ApiCommandException((Error) res));
|
||||||
}
|
}
|
||||||
listeners.remove(res.getTag());
|
|
||||||
} else if (res instanceof Error) {
|
|
||||||
l.error(new ApiCommandException((Error) res));
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
nextTag();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -276,7 +280,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private void nextLine() throws ApiConnectionException, ApiDataException {
|
private void nextLine() throws ApiConnectionException, ApiDataException {
|
||||||
if (lines.isEmpty()) {
|
if (lines.isEmpty()) {
|
||||||
String block = reader.take();
|
String block = reader.take();
|
||||||
String parts[] = block.split("\n");
|
String[] parts = block.split("\n");
|
||||||
lines.addAll(Arrays.asList(parts));
|
lines.addAll(Arrays.asList(parts));
|
||||||
}
|
}
|
||||||
line = lines.remove(0);
|
line = lines.remove(0);
|
||||||
|
|
@ -289,7 +293,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private String peekLine() throws ApiConnectionException, ApiDataException {
|
private String peekLine() throws ApiConnectionException, ApiDataException {
|
||||||
if (lines.isEmpty()) {
|
if (lines.isEmpty()) {
|
||||||
String block = reader.take();
|
String block = reader.take();
|
||||||
String parts[] = block.split("\n");
|
String[] parts = block.split("\n");
|
||||||
lines.addAll(Arrays.asList(parts));
|
lines.addAll(Arrays.asList(parts));
|
||||||
}
|
}
|
||||||
return lines.get(0);
|
return lines.get(0);
|
||||||
|
|
@ -302,10 +306,10 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
switch (line) {
|
switch (line) {
|
||||||
case "!re":
|
case "!re":
|
||||||
return unpackRe();
|
return unpackRe();
|
||||||
|
case "!empty":
|
||||||
case "!done":
|
case "!done":
|
||||||
return unpackDone();
|
return unpackDone();
|
||||||
case "!trap":
|
case "!trap":
|
||||||
return unpackError();
|
|
||||||
case "!halt":
|
case "!halt":
|
||||||
return unpackError();
|
return unpackError();
|
||||||
case "":
|
case "":
|
||||||
|
|
@ -316,12 +320,10 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
|
|
||||||
private Result unpackRe() throws ApiDataException, ApiConnectionException {
|
private Result unpackRe() throws ApiDataException, ApiConnectionException {
|
||||||
nextLine();
|
nextLine();
|
||||||
int l = 0;
|
|
||||||
Result res = new Result();
|
Result res = new Result();
|
||||||
while (!line.startsWith(("!"))) {
|
while (!line.startsWith(("!"))) {
|
||||||
l++;
|
|
||||||
if (line.startsWith(("="))) {
|
if (line.startsWith(("="))) {
|
||||||
String parts[] = line.split("=", 3);
|
String[] parts = line.split("=", 3);
|
||||||
if (parts.length == 3) {
|
if (parts.length == 3) {
|
||||||
if (!parts[2].endsWith("\r")) {
|
if (!parts[2].endsWith("\r")) {
|
||||||
res.put(parts[1], unpackResult(parts[2]));
|
res.put(parts[1], unpackResult(parts[2]));
|
||||||
|
|
@ -338,7 +340,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
throw new ApiDataException(String.format("Malformed line '%s'", line));
|
throw new ApiDataException(String.format("Malformed line '%s'", line));
|
||||||
}
|
}
|
||||||
} else if (line.startsWith(".tag=")) {
|
} else if (line.startsWith(".tag=")) {
|
||||||
String parts[] = line.split("=", 2);
|
String[] parts = line.split("=", 2);
|
||||||
if (parts.length == 2) {
|
if (parts.length == 2) {
|
||||||
res.setTag(parts[1]);
|
res.setTag(parts[1]);
|
||||||
}
|
}
|
||||||
|
|
@ -379,12 +381,12 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
|
|
||||||
while (!line.startsWith("!")) {
|
while (!line.startsWith("!")) {
|
||||||
if (line.startsWith(".tag=")) {
|
if (line.startsWith(".tag=")) {
|
||||||
String parts[] = line.split("=", 2);
|
String[] parts = line.split("=", 2);
|
||||||
if (parts.length == 2) {
|
if (parts.length == 2) {
|
||||||
done.setTag(parts[1]);
|
done.setTag(parts[1]);
|
||||||
}
|
}
|
||||||
} else if (line.startsWith(("=ret"))) {
|
} else if (line.startsWith(("=ret"))) {
|
||||||
String parts[] = line.split("=", 3);
|
String[] parts = line.split("=", 3);
|
||||||
if (parts.length == 3) {
|
if (parts.length == 3) {
|
||||||
done.setHash(parts[2]);
|
done.setHash(parts[2]);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -408,7 +410,7 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
if (hasNextLine()) {
|
if (hasNextLine()) {
|
||||||
while (!line.startsWith("!")) {
|
while (!line.startsWith("!")) {
|
||||||
if (line.startsWith(".tag=")) {
|
if (line.startsWith(".tag=")) {
|
||||||
String parts[] = line.split("=", 2);
|
String[] parts = line.split("=", 2);
|
||||||
if (parts.length == 2) {
|
if (parts.length == 2) {
|
||||||
err.setTag(parts[1]);
|
err.setTag(parts[1]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue