import librouteros api = librouteros.connect( host='192.168.88.1', username='admin', password='', port=8728, # default API port (plaintext) use_ssl=False ) resources = api(cmd='/system/resource/print') print(f"Board: {resources[0]['board-name']}") print(f"Uptime: {resources[0]['uptime']}") print(f"CPU Load: {resources[0]['cpu-load']}%")
conns = api(cmd='/ip/firewall/connection/print') tcp_count = sum(1 for c in conns if c['protocol'] == 'tcp') udp_count = sum(1 for c in conns if c['protocol'] == 'udp') mikrotik api examples
print(f"Active connections: TCP={tcp_count}, UDP={udp_count}") Limit a client’s bandwidth via script. import librouteros api = librouteros
If you manage more than one MikroTik router, logging into WinBox or WebFig for every small change gets old fast. The MikroTik API lets you script configuration, gather data, and react to network events — all from your own code. Let me know in the comments
Let me know in the comments. Want the code as a ready-to-use Python script? Download the gist here.
import ssl ssl_context = ssl.create_default_context() api_ssl = librouteros.connect( host='192.168.88.1', username='admin', password='', port=8729, use_ssl=True, ssl_wrapper=ssl_context )
api(cmd='/queue/simple/add', name='client-limited', target='192.168.88.100/32', max_limit='5M/5M', comment='api-created') For production, always use SSL on port 8729.