Mikrotik Api Examples Site

interfaces = connection.path('interface').select('name', 'type', 'running') for iface in interfaces: print(iface)


For network engineers and system administrators, managing a fleet of MikroTik routers (RouterOS) via the graphical WinBox or WebFig interface is efficient for one-off tasks. However, when you need to provision 100 routers, dynamically update firewall filters based on an external threat feed, or automate bandwidth changes based on the time of day, the command-line interface (CLI) and GUI fall short.

Enter the MikroTik API.

The MikroTik API (based on a plain-text, TCP-based protocol) allows you to execute commands, configure settings, and retrieve data programmatically using scripts, Python, Go, or PHP. This article provides a deep dive into practical, real-world MikroTik API examples.

api('/system/backup/save', 'name': 'api_backup') mikrotik api examples

MikroTik RouterOS API (port 8728, or 8729 for TLS) uses a simple line‑based, synchronous protocol. Each command is sent as a set of key=value pairs followed by an empty line. Responses are similarly structured, ending with !done or !trap (error).


# Disable an interface
api('/interface/set', 
    '.id': 'ether2',
    'disabled': 'yes'
)
pip install librouteros

Connect and print interfaces:

import librouteros

connection = librouteros.connect( host='192.168.88.1', username='api_user', password='pass', port=8728 )

resources = connection.path('system', 'resource').get()
print(f"CPU Load: resources['cpu-load']%")
print(f"Uptime: resources['uptime']")