Java 获取ROS 数据
官方网站
git地址
https://github.com/GideonLeGrange/mikrotik-java
<dependency> <groupId>me.legrange</groupId> <artifactId>mikrotik</artifactId> <version>3.0.7</version> </dependency>
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); } }

点开之后会发现它与mysql表没什么区别,属于行列形态的,如下:
(我不能把下面的数据展示出来,知道像Mysql一样就行了)
![]()
winBox命令:是怎么查询数据的
打开一个Terminal,如下。你需要进入ppp/secret下 才能。

回车一下,就能打印出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(); }
本文来自博客园,作者:无上仰无,转载请注明原文链接:https://www.cnblogs.com/sharey/articles/14989754.html

浙公网安备 33010602011771号