1 unstable release
0.1.0 | Oct 19, 2023 |
---|
#3 in #software
36KB
397 lines
MikrotikLite
A Rust library for interacting with Mikrotik routers.
Features
- Simple and easy-to-use: The library provides a simple and straightforward interface for interacting with Mikrotik routers.
- Asynchronous: The library is asynchronous, making it efficient for long-running operations.
- Does not support event listening: The library does not support event listening. This is because it would make the library more complex and difficult to use. It would also create problems when working with concurrency. In the future, event listening may be added, but it is not currently planned.
Example: Retrieving Data in JSON Format
use mikrotik_lite::MikrotikLite;
let mut api = MikrotikLite::new(
"api_login".to_string(),
"api_pass".to_string(),
"10.80.80.1:8728".to_string(),
).unwrap();
// Get the current user identity.
let identity_json = api.send_command_json("/system/identity/print".to_string(), None).unwrap();
// Print the JSON string.
println!("{}", identity_json);
You can predefine a structure and parse the response into that structure:
use serde::Deserialize;
use mikrotik_lite::MikrotikLite;
// `Identity` struct
#[derive(Debug, Deserialize)]
struct Identity {
pub name: String
}
let mut api = MikrotikLite::new(
"api_login".to_string(),
"api_pass".to_string(),
"10.80.80.1:8728".to_string(),
).unwrap();
let identity = api.send_command::<Identity>(
"/system/identity/print".to_string(),
None
).unwrap();
println!("{:#?}", identity);
This library does not provide predefined structures, but you can easily create your own structures by sending requests through send_command_json
and creating structures based on the fields you need. Serialization and deserialization are handled using the serde
library. There are some considerations for handling specific field types, as MikroTik does not provide type markers in their responses. You should use deserialize_bool_from_anything
for Boolean values and deserialize_string_from_number
for numeric values.
Handling Boolean values #[serde(deserialize_with = "deserialize_bool_from_anything")]
Handling numeric values #[serde(deserialize_with = "deserialize_string_from_number")]
Additionally, some field names do not follow Rust naming conventions and should be renamed using the serde(rename = "dst-address")
attribute.
In the examples
directory, you can find usage examples for the library.
All methods have asynchronous counterparts with _async
suffixes, allowing you to use Tokio for concurrent requests. Here's an example:
use mikrotik_lite::MikrotikLite;
let mut api1 = MikrotikLite::new(
"api_login".to_string(),
"api_pass".to_string(),
"10.80.80.1:8728".to_string(),
).unwrap();
let mut api2 = MikrotikLite::new(
"api_login".to_string(),
"api_pass".to_string(),
"10.80.80.1:8728".to_string(),
).unwrap();
let route_future = api1.send_command_json_async(
"/ip/route/print".to_string(),
None
);
let identity_future = api2.send_command_json_async(
"/system/identity/print".to_string(),
None
);
async {
let (route, identity) = tokio::join!(route_future, identity_future);
println!("{}", route.unwrap());
println!("{}", identity.unwrap());
};
You can pass parameters with your commands like this:
use std::collections::HashMap;
use mikrotik_lite::MikrotikLite;
let mut api = MikrotikLite::new(
"api_login".to_string(),
"api_pass".to_string(),
"10.80.80.1:8728".to_string(),
).unwrap();
let mut attr = HashMap::new();
attr.insert("address".to_string(), "8.8.8.8".to_string());
attr.insert("count".to_string(), "5".to_string());
let ping = api.send_command_json(
"/ping".to_string(),
Some(attr.clone())
).unwrap();
println!("{}", ping);
Dependencies
~2.5–9MB
~68K SLoC