Java 获取ROS 数据

官方网站

http://www.mikrotik.com/

 

git地址

https://github.com/GideonLeGrange/mikrotik-java

 

Jar包

 

<dependency>
    <groupId>me.legrange</groupId>
    <artifactId>mikrotik</artifactId>
    <version>3.0.7</version>
</dependency>

 

与Ros通信至少需要哪些参数?

 

host: {}, port: {}, rosUserName: {}, rosPassword: {},Socket:{}

 

建立连接并登录

 

以下代码是手写的,可能会有差错。如果有异常,请正常抛出或者记录。

 

//不要忽略timeout
public ApiConnection getRosConnection(){
    
    //建立连接超时时间限制
    ApiConnection con =  ApiConnection.connect(SocketFactory.getDefault(), host, port, 3000);
    //命令执行超时时间限制
    conn.setTimeout(5000);
    
    conn.login(rosUserName, rosPassword);
    
    return conn;
    
}

 

执行命令

 

public List<Map<String, String>> execute(ApiConnection conn, String command) {

        try {
            return conn.execute(command);
        } catch (MikrotikApiException e) {
            log.error("", e);
        }
    
}

 

退出连接

 

public void logout(ApiConnection conn) {
    
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (ApiConnectionException e) {
            log.error("", e);
        }
    }

 

WinBox直观:查看ROS信息

 

 

 

点开之后会发现它与mysql表没什么区别,属于行列形态的,如下:

(我不能把下面的数据展示出来,知道像Mysql一样就行了)



 

 

winBox命令:是怎么查询数据的

 

打开一个Terminal,如下。你需要进入ppp/secret下 才能。

 

 

回车一下,就能打印出secret下的所有数据了。

想要知道更多可以去官网或者,在控制台上打出个? ,问号。



 

Java命令:获取ROS信息

 

注意,我执行的都是secret 栏目下的

 

查询

 

public static R readAllCallerIdFromActiveConnections(

          String queryUser = "/ppp/secret/print";
    
          ApiConnection conn = login(host, port, rosLoginUserName, rosLoginPassword);

          List<Map<String, String>> existsList = execute(conn, String.format(queryUser));

          return R.ok().data("list",exestList);

}         

 

创建

 

public static R createCallerIdToActiveConnections(String remote-address,String password, String service,String comment){

      String createUser = "/ppp/secret/add name=%s password=%s remote-address=%s service=%s comment=%s";

    ApiConnection conn = login(host, port, rosLoginUserName, rosLoginPassword);
    
    conn.execute(String.format(createUser, userName, password, mngtIp, service, comment)); 

    return R.ok();
} 

 

修改

public static R updateCallerIdToActiveConnections(String id,String remote-address,String password, String service,String comment){

     String updateUser = "/ppp/secret/set .id= %s name=%s password=%s remote-address=%s service=%s comment=%s";

    ApiConnection conn = login(host, port, rosLoginUserName, rosLoginPassword);
    
    conn.execute(String.format(createUser, userName, password, mngtIp, service, comment)); 

    return R.ok();
} 

 

 

删除

 

public static R removeCallerIdToActiveConnections(String host, int port, String rosLoginUserName, String rosLoginPassword, String userName){

      String removeUser = "/ppp/secret/remove numbers= %s";

      ApiConnection conn = login(host, port, rosLoginUserName, rosLoginPassword);
    
    execute(conn, String.format(removeUser, userName));

    return R.ok();
} 

 

posted @ 2021-07-09 11:18  无上仰无  阅读(817)  评论(0)    收藏  举报