Skip to main content

Python API to RouterBoard devices produced by MikroTik.

Project description

RouterOS-api Fork from Social WiFi.

Build Status Latest Version Supported Python versions Wheel Status License

Python API to RouterBoard devices produced by MikroTik written by Social WiFi.

Social WiFi logo

Added usage of ipv6 connection soket. By adding this, error in connection with routeros 7.* disappear.

Usage

Connection

#!/usr/bin/python

import routeros_api

connection = routeros_api.RouterOsApiPool('IP', username='admin', password='')
api = connection.get_api()

Connect Options

routeros_api.RouterOsApiPool(
    host,
    username='admin',
    password='',
    port=8728,
    use_ssl=False,
    ssl_verify=True,
    ssl_verify_hostname=True,
    ssl_context=None,
)

Parameters:

  • host - String - Hostname or IP of device

Optional Parameters:

  • username - String - Login username - Default 'admin'
  • password - String - Login password - Default empty string
  • port - Integer - TCP Port for API - Default 8728 or 8729 when using SSL
  • plaintext_login - Boolean - Try plaintext login (for RouterOS 6.43 onwards) - Default False
  • use_ssl - Boolean - Use SSL or not? - Default False
  • ssl_verify - Boolean - Verify the SSL certificate? - Default True
  • ssl_verify_hostname - Boolean - Verify the SSL certificate hostname matches? - Default True
  • ssl_context - Object - Pass in a custom SSL context object. Overrides other options. - Default None

Using SSL

If we want to use SSL, we can simply specify use_ssl as True:

connection = routeros_api.RouterOsApiPool('<IP>', username='admin', password='', use_ssl=True)

This will automatically verify SSL certificate and hostname. The most flexible way to modify SSL parameters is to provide an SSL Context object using the ssl_context parameter, but for typical use-cases with self-signed certificates, the shorthand options of ssl_verify and ssl_verify_hostname are provided.

e.g. if using a self-signed certificate, you can (but probably shouldn't) use:

connection = routeros_api.RouterOsApiPool(
    '<IP>',
    username='admin',
    password='',
    use_ssl=True,
    ssl_verify=False,
    ssl_verify_hostname=False,
)

Login for RouterOS v6.43 onwards

RouterOS Versions v6.43 onwards now use a different login method. The disadvantage is that it passes the password in plain text. For security we only attempt the plaintext login if requested using the plaintext_login parameter. It is highly recommended only to use this option with SSL enabled.

routeros_api.RouterOsApiPool(host, username='admin', password='', plaintext_login=True)

Execute Commands

Call this with a resource and parameters as name/value pairs.

api.get_binary_resource('/').call('<resource>',{ <dict of params> })

Examples

api.get_binary_resource('/').call('tool/fetch',{ 'url': "https://dummy.url" })
api.get_binary_resource('/').call('ping', { 'address': '192.168.56.1', 'count': '4' })

Fetch List/Resource

list = api.get_resource('/command')

Example

list_queues = api.get_resource('/queue/simple')

Show all elements

list_queues.get()

Add rules

list.add(attribute="vale", attribute_n="value")

NOTE: Atributes with -, like max-limit use underscore _: max_limit

Example:

list_queues.add(name="001", max_limit="512k/4M", target="192.168.10.1/32")

Update Values

list.set(id, attributes)

Example:

list_queues.set(id="*2", name="jhon")

Get element:

list.get(attribute=value)

Example:

list_queues.get(name="jhon")

Remove element:

list.remove(id)

Example:

list_queues.remove(id="*2")

Close conection:

connection.disconnect()

Other Example:

list_address =  api.get_resource('/ip/firewall/address-list')
list_address.add(address="192.168.0.1",comment="P1",list="10M")

list_address.get(comment="P1")

list_address.remove(id="*7")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

RouterOS-7*-api-0.18.1.dev0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

RouterOS_7_api-0.18.1.dev0-py2.py3-none-any.whl (21.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file RouterOS-7*-api-0.18.1.dev0.tar.gz.

File metadata

  • Download URL: RouterOS-7*-api-0.18.1.dev0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for RouterOS-7*-api-0.18.1.dev0.tar.gz
Algorithm Hash digest
SHA256 13edcbd5d739302c3c6b0d1b4392237246ad46c6b6a4d2194e3308f327e5d22b
MD5 4319c6d07d9d93d59ce818ac14a86c17
BLAKE2b-256 07d8d66473a35301a3a0339d6a292508933c17352874d282323411ce148fce8d

See more details on using hashes here.

File details

Details for the file RouterOS_7_api-0.18.1.dev0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for RouterOS_7_api-0.18.1.dev0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2cf4a9211f842f5a4f09c504299819a3591bfe396db8bd6cd3ae9410a4284511
MD5 d4997fefd0e99d355f840bebeeb1ffed
BLAKE2b-256 55afd42d4be25f54d972d55cf8259a6b3f7027e7a52496b95bc3af154e9cf272

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page