mikrotik_rs

Struct MikrotikDevice

Source
pub struct MikrotikDevice(/* private fields */);
Expand description

A client for interacting with MikroTik devices.

The MikrotikDevice struct provides an asynchronous interface for connecting to a MikroTik device and sending commands to it. It encapsulates the communication with the device through a background actor that handles the connection and command execution. Can be cheaply cloned to share the same connection across multiple threads.

Implementations§

Source§

impl MikrotikDevice

Source

pub async fn connect<A: ToSocketAddrs>( addr: A, username: &str, password: Option<&str>, ) -> DeviceResult<Self>

Asynchronously establishes a connection to a MikroTik device.

This function initializes the connection to the MikroTik device by starting a DeviceConnectionActor and returns an instance of MikrotikDevice that can be used to send commands to the device.

§Parameters
  • addr: The address of the MikroTik device. This can be an IP address or a hostname.
  • username: The username for authenticating with the device.
  • password: An optional password for authentication. If None, no password will be sent.
§Returns
  • Ok(Self): An instance of MikrotikDevice on successful connection.
  • Err(io::Error): An error if the connection could not be established.
§Examples
let device = MikrotikDevice::connect("192.168.88.1:8728", "admin", Some("password")).await?;
§Attention 🚨

The connection to the MikroTik device is not encrypted (plaintext API connection over 8728/tcp port). In the future, support for encrypted connections (e.g., API-SSL) will be added.

Source

pub async fn send_command( &self, command: Command, ) -> Receiver<DeviceResult<CommandResponse>>

Asynchronously sends a command to the connected MikroTik device and returns a receiver for the response.

This method allows sending commands to the MikroTik device and provides an asynchronous channel (receiver) that will receive the command execution results.

§Parameters
  • command: The Command to send to the device, consisting of a tag and data associated with the command.
§Returns

A mpsc::Receiver that can be awaited to receive the response to the command. Responses are wrapped in [io::Result] to handle any I/O related errors during command execution or response retrieval.

§Panics

This method panics if sending the command message to the DeviceConnectionActor fails, which could occur if the actor has been dropped or the channel is disconnected.

§Examples
let command = CommandBuilder::new().command("/interface/print").build();
let mut response_rx = device.send_command(command).await;

while let Some(response) = response_rx.recv().await {
    println!("{:?}", response?);
}

Trait Implementations§

Source§

impl Clone for MikrotikDevice

Source§

fn clone(&self) -> MikrotikDevice

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.