Mikrotik Api - Examples
import createClient from '@hertzg/routeros-api';
// Retrieve all interfaces $api->write('/interface/print'); $interfaces = $api->read(); print_r($interfaces);
scripts = api.path('system', 'script') new_script = scripts.add( name='daily_reboot', source=':log info "Rebooting via API script"; /system reboot', policy='read,write,reboot' ) scripts.call('run', .id=new_script['.id']) mikrotik api examples
(Simplified representation of the socket stream)
let result = device.command("/interface/print").await?; println!("Result: :?", result); import createClient from '@hertzg/routeros-api'
This example demonstrates how to pass arguments to create a static DHCP lease configuration.
ROUTER_IP = '192.168.88.1' API_USER = 'api_user' API_PASS = 'api_pass' CLOUDFLARE_ZONE = 'your_zone_id' CLOUDFLARE_RECORD = 'your_record_id' CLOUDFLARE_TOKEN = 'bearer_token' // Retrieve all interfaces $api->
import routeros_api def get_router_resources(host, username, password): # Establish connection connection = routeros_api.RouterOsApiPool( host, username=username, password=password, plaintext_login=True ) api = connection.get_api() # Navigate to system resource path resource_path = api.get_resource('/system/resource') resources = resource_path.get() if resources: data = resources[0] print("--- MikroTik System Resources ---") print(f"Uptime: data.get('uptime')") print(f"Version: data.get('version')") print(f"Board Name: data.get('board-name')") print(f"CPU Load: data.get('cpu-load')%") print(f"Free Memory: int(data.get('free-memory', 0)) / 1024 / 1024:.2f MB") else: print("Could not retrieve resources.") connection.disconnect() # Usage get_router_resources('192.168.88.1', 'api_user', 'YourSecurePassword123!') Use code with caution. Example 2: Adding a DHCP Server Lease