device.connect('admin', 'password').then(function(conn) // Add an IP address to ether1 conn.write('/ip/address/add', ['=address=192.168.88.1/24', '=interface=ether1']);
Mastering the MikroTik API: Practical Examples for Network Automation
To use REST API, the www-ssl service (or www from RouterOS v7.9+) must be enabled. mikrotik api examples
Python is the most popular language for network automation. The routeros_api library provides a high-level, Pythonic interface for interacting with RouterOS. Installation pip install routeros_api Use code with caution. Example 1: Fetching System Resource Information
PHP is ideal for creating web-based ISP portal interfaces or custom network monitoring dashboards. We will use the popular RouterOS API PHP class wrapper. device
def add_to_whitelist(api, ip_address): firewall_list = api.path('/ip/firewall/address-list') firewall_list.add(list='Authorized_Users', address=ip_address, comment='Added via API') print(f"IP ip_address added to whitelist.") Use code with caution. 4. PHP API Example: Web Dashboard Integration
The standard API uses a sentence-based messaging system over raw TCP. Commands follow the CLI structure but use a specific syntax for attributes. Installation pip install routeros_api Use code with caution
In this example, we'll create a new user using the Mikrotik API in PHP.