Mikrotik Api Examples -

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.

Remember to always test automation scripts in a lab environment before deploying to production routers. Now go automate your MikroTik network! mikrotik api examples

# Bad: retrieves all 100+ fields rules = api.path('ip', 'firewall', 'filter') rules = api.path('ip', 'firewall', 'filter').select('dst-port', 'action') Idempotent Operations Before adding a rule, check if it exists: For network engineers and system administrators, managing a

queue = api.path('queue', 'simple').add( name='customer-001', target='192.168.88.100/32', max_limit='5M/2M', # upload/download comment='API traffic limit' ) api.path('ip', 'firewall', 'filter').add( chain='forward', src_address='5.5.5.5', action='drop', comment='Blocked by API automation' ) Example 6: Disable/Enable an Interface # Find the interface interfaces = api.path('interface') for interface in interfaces: if interface['name'] == 'ether2': # Disable it interfaces.update(interface['.id'], disabled='yes') # Later, enable it # interfaces.update(interface['.id'], disabled='no') 6. Monitoring & Data Retrieval Examples Example 7: Get Resource Usage (CPU, Memory) response = api.path('system', 'resource').get() resource = next(response) # Returns an iterator print(f"CPU Load: {resource['cpu-load']}%") print(f"Free Memory: {int(resource['free-memory'])/1024/1024:.2f} MB") print(f"Uptime: {resource['uptime']}") Example 8: Show Active Wireless Clients registrations = api.path('interface', 'wireless', 'registration-table') for client in registrations: print(f"MAC: {client['mac-address']}, Signal: {client['signal-strength']} dBm, TX Rate: {client['tx-rate']}") Example 9: Retrieve Logs logs = api.path('log').select('time', 'topics', 'message') for log in logs: if 'error' in log['topics'].lower(): print(f"ERROR: {log['time']} - {log['message']}") 7. Advanced Operations Example 10: Run a Script Stored on the Router Assume you have a script named "backup_config" in /system script . Now go automate your MikroTik network

api = librouteros.connect(...) address_list = api.path('ip', 'firewall', 'address-list') logs = api.path('log').select('time', 'message') for log in logs: if 'ssh' in log['message'].lower() and 'failure' in log['message'].lower(): # Extract IP using regex (pseudo) import re match = re.search(r'from (\d+.\d+.\d+.\d+)', log['message']) if match: ip = match.group(1) # Check if already listed existing = list(address_list.select(list='ssh_block', address=ip)) if not existing: address_list.add(list='ssh_block', address=ip, timeout='1h') print(f"Banned {ip} for 1 hour") Use Case 3: Bandwidth Time-of-Day Scheduler Change queue max-limit at 9 AM and 6 PM.

Installation:

if == ' main ': main() Use Case 2: Auto-Ban SSH Brute Force Attempts Read failed login attempts from logs and add source IPs to an address list.

COPYRIGHT © 2009-2025 ITJUSTGOOD.COM