151 lines
4.5 KiB
Java
151 lines
4.5 KiB
Java
package com.eightOceans.mikroTik.sample;
|
|
|
|
import com.eightOceans.mikroTik.ApiConnection;
|
|
import com.eightOceans.mikroTik.ReadDataResult;
|
|
import com.eightOceans.mikroTik.Result;
|
|
import com.eightOceans.mikroTik.SSLMode;
|
|
import com.eightOceans.mikroTik.ops.AccessListEntry;
|
|
import com.eightOceans.mikroTik.ops.OPs;
|
|
import java.util.List;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.Future;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
/**
|
|
*
|
|
* @author mh
|
|
*/
|
|
public class TestApp {
|
|
|
|
private static String user = "admin";
|
|
private static String pass = "";
|
|
private static String host = "192.168.61.254";
|
|
private static int port = 8729;
|
|
|
|
|
|
/**
|
|
*
|
|
* @param <T>
|
|
* @param f
|
|
* @param timeout
|
|
* @param startOp
|
|
* @param resOp
|
|
* @return On sucess the Result of the Future, on failure Result of the
|
|
* Future or a basic self generated Result-instance.
|
|
*/
|
|
private static <T extends Result> T run(Future<T> f, long timeout, String opInProgressDescr) {
|
|
if (opInProgressDescr != null) {
|
|
System.out.println("* " + opInProgressDescr + " ...");
|
|
}
|
|
boolean succ;
|
|
String msg;
|
|
T res = null;
|
|
try {
|
|
res = f.get(timeout, TimeUnit.SECONDS);
|
|
msg = res.ResultMessage;
|
|
succ = res.Success;
|
|
} catch (InterruptedException | ExecutionException | TimeoutException iex) {
|
|
succ = false;
|
|
msg = "Operation interrupted/timeout:" + iex.getMessage();
|
|
}
|
|
if (msg != null) {
|
|
if (succ) {
|
|
System.out.println("- Operation succeeded: " + msg + ".");
|
|
} else {
|
|
System.out.println("! Operation FAILED: " + msg + "!");
|
|
}
|
|
}
|
|
if (res == null) {
|
|
res = (T) new Result();
|
|
res.Success = succ;
|
|
res.ResultMessage = msg;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
|
|
enum NextOp {
|
|
|
|
CONNECT,
|
|
LOGIN,
|
|
TEST1,
|
|
TEST2,
|
|
EXIT,
|
|
UNDEF
|
|
|
|
};
|
|
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println("- Starting test app");
|
|
|
|
if (args.length >= 3) {
|
|
host = args[0];
|
|
user = args[1];
|
|
pass = args[2];
|
|
}
|
|
|
|
ApiConnection conn = new ApiConnection();
|
|
|
|
NextOp op = NextOp.CONNECT;
|
|
while (true) {
|
|
|
|
NextOp nextOp = NextOp.EXIT;
|
|
|
|
switch (op) {
|
|
case CONNECT:
|
|
if (run(conn.open(host, port, SSLMode.Weak), 7, "Establishing connection").Success) {
|
|
nextOp = NextOp.LOGIN;
|
|
}
|
|
break;
|
|
case LOGIN:
|
|
if (run(conn.login(user, pass), 7, "Loggin in").Success) {
|
|
nextOp = NextOp.TEST2;
|
|
}
|
|
break;
|
|
case TEST1:
|
|
conn.sendCommand("/interface/getall");
|
|
String d = "";
|
|
while ((d == null || (d.indexOf("!done") == -1))) {
|
|
Result res = run(conn.readData(), 7, null);
|
|
if (res.Success) {
|
|
d = ((ReadDataResult) res).Data;
|
|
System.out.println(" Got: " + d);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
System.out.println(" Reply complete");
|
|
nextOp = NextOp.TEST2;
|
|
break;
|
|
case TEST2:
|
|
try {
|
|
OPs ops = new OPs(conn);
|
|
Result<List<AccessListEntry>> r = ops.getWireless().getAccessListEntries().get();
|
|
System.out.println(r.Result.get(0).preSharedKey);
|
|
System.out.println(r.Result.get(0).privateKey);
|
|
System.out.println("XXX");
|
|
} catch (Throwable th) {
|
|
th.printStackTrace();
|
|
}
|
|
|
|
break;
|
|
case EXIT:
|
|
run(conn.close(), 7, "Closing connection");
|
|
System.out.println("Test app done");
|
|
//
|
|
// for (int i = 0; i < 100; i++) {
|
|
// System.out.println(Utils.createPresharedWifiKey(12, 13, 1, 2));
|
|
// }
|
|
System.exit(0);
|
|
}
|
|
op = nextOp;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|