Skip to content

RouterOS

RouterOS

Bases: Ip, System

Class that generates the connection with a MikroTik router.

Examples:

>>> from netmikro import RouterOS
>>> router = RouterOS(
...     '192.168.3.3',
...     'user',
...     'password',
...     22,
... )
>>> router.cmd('/system identity print')
'name: Netmikro'
Source code in netmikro/routeros.py
class RouterOS(Ip, System):
    """Class that generates the connection with a MikroTik router.

    Examples:
        >>> from netmikro import RouterOS
        >>> router = RouterOS(
        ...     '192.168.3.3',
        ...     'user',
        ...     'password',
        ...     22,
        ... )
        >>> router.cmd('/system identity print')
        'name: Netmikro'
    """

    def __init__(
        self,
        host: str,
        username: str,
        password: str,
        ssh_port: int = 22,
        delay: float = 0,
    ):
        """Class that generates the connection with a MikroTik router.

        Args:
            host (str): IP address of the router you want to connect to.
            username (str): Username to be used in the connection.
            password (str): Password to be used in the connection.
            ssh_port (int): SSH port to be used in the connection.
            delay (float): Time delay between command executions on the router.
        """
        super().__init__(host, username, password, ssh_port, delay)

        self._username = username
        self._host = host

    def disconnect(self):
        """Disconnects the connection with the router.

        Examples:
            >>> router.disconnect()
        """
        return self._connection.disconnect()

    def cmd(self, command: str) -> str:
        """Runs a command in the router's terminal.

        Args:
            command (str): Command to be executed.

        Returns:
            str: Output of the command

        Examples:
            >>> router.cmd('/system identity print')
            'name: Netmikro'
        """
        # The `expect_string` parameter is a regex (format: [admin@mikrotik])
        # necessary in case the router's identity is changed,
        # there is no ReadTimeout error due to the output format changing,
        # as it includes the router's identity
        return self._connection.send_command(
            command_string=command,
            expect_string=rf'\[{self._username}@[^]]+\]',
        )

    def cmd_multiline(self, *args) -> str:
        """Runs multiple commands in the router's terminal.

        Args:
            *args (str): List of commands to be executed.

        Returns:
            str: Output of the commands.

        Examples:
            >>> router.cmd_multiline([
            ...     '/system identity print',
            ...     '/system note print'
            ... ])
            ['name: Netmikro', 'note: Test']
        """
        commands = [x for x in args]
        return self._connection.send_multiline(commands)

__init__(host, username, password, ssh_port=22, delay=0)

Class that generates the connection with a MikroTik router.

Parameters:

Name Type Description Default
host str

IP address of the router you want to connect to.

required
username str

Username to be used in the connection.

required
password str

Password to be used in the connection.

required
ssh_port int

SSH port to be used in the connection.

22
delay float

Time delay between command executions on the router.

0
Source code in netmikro/routeros.py
def __init__(
    self,
    host: str,
    username: str,
    password: str,
    ssh_port: int = 22,
    delay: float = 0,
):
    """Class that generates the connection with a MikroTik router.

    Args:
        host (str): IP address of the router you want to connect to.
        username (str): Username to be used in the connection.
        password (str): Password to be used in the connection.
        ssh_port (int): SSH port to be used in the connection.
        delay (float): Time delay between command executions on the router.
    """
    super().__init__(host, username, password, ssh_port, delay)

    self._username = username
    self._host = host

cmd(command)

Runs a command in the router's terminal.

Parameters:

Name Type Description Default
command str

Command to be executed.

required

Returns:

Name Type Description
str str

Output of the command

Examples:

>>> router.cmd('/system identity print')
'name: Netmikro'
Source code in netmikro/routeros.py
def cmd(self, command: str) -> str:
    """Runs a command in the router's terminal.

    Args:
        command (str): Command to be executed.

    Returns:
        str: Output of the command

    Examples:
        >>> router.cmd('/system identity print')
        'name: Netmikro'
    """
    # The `expect_string` parameter is a regex (format: [admin@mikrotik])
    # necessary in case the router's identity is changed,
    # there is no ReadTimeout error due to the output format changing,
    # as it includes the router's identity
    return self._connection.send_command(
        command_string=command,
        expect_string=rf'\[{self._username}@[^]]+\]',
    )

cmd_multiline(*args)

Runs multiple commands in the router's terminal.

Parameters:

Name Type Description Default
*args str

List of commands to be executed.

()

Returns:

Name Type Description
str str

Output of the commands.

Examples:

>>> router.cmd_multiline([
...     '/system identity print',
...     '/system note print'
... ])
['name: Netmikro', 'note: Test']
Source code in netmikro/routeros.py
def cmd_multiline(self, *args) -> str:
    """Runs multiple commands in the router's terminal.

    Args:
        *args (str): List of commands to be executed.

    Returns:
        str: Output of the commands.

    Examples:
        >>> router.cmd_multiline([
        ...     '/system identity print',
        ...     '/system note print'
        ... ])
        ['name: Netmikro', 'note: Test']
    """
    commands = [x for x in args]
    return self._connection.send_multiline(commands)

disconnect()

Disconnects the connection with the router.

Examples:

>>> router.disconnect()
Source code in netmikro/routeros.py
def disconnect(self):
    """Disconnects the connection with the router.

    Examples:
        >>> router.disconnect()
    """
    return self._connection.disconnect()