Small tweaks
This commit is contained in:
parent
b617aa12c8
commit
473f303add
|
|
@ -9,7 +9,7 @@ public class Config {
|
|||
|
||||
public static final String USERNAME = "admin";
|
||||
public static final String PASSWORD = "";
|
||||
public static final String HOST = "10.0.0.1";
|
||||
public static final String HOST = "10.0.1.3";
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ public class ApiConnection {
|
|||
|
||||
/**
|
||||
* Create a new API connection to the give device on the supplied port
|
||||
* @param host The host to which to connect.
|
||||
* @param port The TCP port to use.
|
||||
* @return The ApiConnection
|
||||
*/
|
||||
public static ApiConnection connect(String host, int port) throws ApiConnectionException {
|
||||
ApiConnection con = new ApiConnection();
|
||||
|
|
@ -32,16 +35,18 @@ public class ApiConnection {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new API connection to the give device on the default API port
|
||||
* Create a new API connection to the give device on the default API port..
|
||||
* @param host The host to which to connect.
|
||||
* @return The ApiConnection
|
||||
*/
|
||||
public static ApiConnection connect(String host) throws ApiConnectionException {
|
||||
public static ApiConnection connect(String host) throws ApiConnectionException {
|
||||
return connect(host, DEFAULT_PORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* State of connection
|
||||
* Check the state of connection.
|
||||
*
|
||||
* @return - if connection is established to router it returns true.
|
||||
* @return if connection is established to router it returns true.
|
||||
*/
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
|
|
@ -63,6 +68,40 @@ public class ApiConnection {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log in to the remote router.
|
||||
*
|
||||
* @param username - username of the user on the router
|
||||
* @param password - password for the user
|
||||
*/
|
||||
public void login(String username, String password) throws MikrotikApiException, ApiCommandException, InterruptedException {
|
||||
List<Result> list = execute("/login");
|
||||
Result res = list.get(0);
|
||||
String hash = res.get("ret");
|
||||
String chal = Util.hexStrToStr("00") + new String(makePass(password)) + Util.hexStrToStr(hash);
|
||||
chal = Util.hashMD5(chal);
|
||||
execute("/login name=" + username + " response=00" + chal);
|
||||
}
|
||||
|
||||
/** execute a command and return a list of results.
|
||||
* @param cmd Command to execute
|
||||
* @return The list of results
|
||||
*/
|
||||
public List<Result> execute(String cmd) throws MikrotikApiException {
|
||||
return execute(Parser.parse(cmd));
|
||||
}
|
||||
|
||||
/** execute a command and attach a result listener to receive it's results.
|
||||
*
|
||||
* @param cmd Command to execute
|
||||
* @param lis ResultListener that will receive the results
|
||||
* @return A command object that can be used to cancel the command.
|
||||
* @throws MikrotikApiException
|
||||
*/
|
||||
public Command execute(String cmd, ResultListener lis) throws MikrotikApiException {
|
||||
return execute(Parser.parse(cmd), lis);
|
||||
}
|
||||
|
||||
/** cancel a command */
|
||||
public void cancel(Command can) throws MikrotikApiException {
|
||||
Command cmd = new Command("/cancel");
|
||||
|
|
@ -70,35 +109,12 @@ public class ApiConnection {
|
|||
execute(cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* set up method that will log you in
|
||||
*
|
||||
* @param name - username of the user on the router
|
||||
* @param password - password for the user
|
||||
* @return
|
||||
*/
|
||||
public void login(String name, String pwd) throws MikrotikApiException, ApiCommandException, InterruptedException {
|
||||
List<Result> list = execute("/login");
|
||||
Result res = list.get(0);
|
||||
String hash = res.get("ret");
|
||||
String chal = Util.hexStrToStr("00") + new String(makePass(pwd)) + Util.hexStrToStr(hash);
|
||||
chal = Util.hashMD5(chal);
|
||||
execute("/login name=" + name + " response=00" + chal);
|
||||
}
|
||||
|
||||
private List<Result> execute(Command cmd) throws MikrotikApiException {
|
||||
SyncListener l = new SyncListener();
|
||||
execute(cmd, l);
|
||||
return l.getResults();
|
||||
}
|
||||
|
||||
public List<Result> execute(String cmd) throws MikrotikApiException {
|
||||
return execute(Parser.parse(cmd));
|
||||
}
|
||||
|
||||
public Command execute(String cmd, ResultListener lis) throws MikrotikApiException {
|
||||
return execute(Parser.parse(cmd), lis);
|
||||
}
|
||||
|
||||
private Command execute(Command cmd, ResultListener lis) throws MikrotikApiException {
|
||||
String tag = nextTag();
|
||||
|
|
@ -424,6 +440,7 @@ public class ApiConnection {
|
|||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<Result> results = new LinkedList<Result>();
|
||||
private Error err;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,6 @@ import java.util.Set;
|
|||
*/
|
||||
public class Result extends Response implements Map<String, String> {
|
||||
|
||||
public Result() {
|
||||
super(null);
|
||||
this.map = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
|
@ -73,6 +68,11 @@ public class Result extends Response implements Map<String, String> {
|
|||
return map.entrySet();
|
||||
}
|
||||
|
||||
Result() {
|
||||
super(null);
|
||||
this.map = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
private final Map<String, String> map;
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue