UNPKG

@fibercom/routeros-api

Version:

Robust MikroTik RouterOS API client for Node.js and TypeScript

226 lines (225 loc) 9.06 kB
import { RouterOSAPI } from "./RouterOSAPI"; import { IRosOptions } from "./IRosOptions"; import { IRosGenericResponse } from "./IRosGenericResponse"; import { RStream } from "./RStream"; /** * Extended RouterOS API class with additional functionality */ export declare class MikrotikAPI extends RouterOSAPI { constructor(options: IRosOptions); /** * Get system information */ getSystemInfo(): Promise<IRosGenericResponse[]>; /** * Get system resources */ getSystemResources(): Promise<IRosGenericResponse[]>; /** * Get interface list */ getInterfaces(): Promise<IRosGenericResponse[]>; /** * Get IP addresses */ getIPAddresses(): Promise<IRosGenericResponse[]>; /** * Adds a static DHCP lease. * * @param {Object} lease - The lease details. * @param {string} lease.address - The IPv4 address to assign. * @param {string} lease.macAddress - The MAC address of the device. * @param {string} lease.server - The name of the DHCP server. * @param {string} [lease.comment] - Optional comment for the lease. * @param {boolean} [lease.disabled] - Optional: Set to true to disable the lease. * @returns {Promise<IRosGenericResponse[]>} The response from the RouterOS API. * @throws {Error} If required parameters are missing or invalid. */ addDhcpLease(lease: { address: string; macAddress: string; server: string; comment?: string; disabled?: boolean; }): Promise<IRosGenericResponse[]>; /** * Retrieve DHCP leases, optionally filtering by DHCP server name. * * @param {string} [serverName] - (Optional) Name of the DHCP server to filter leases. * @returns {Promise<IRosGenericResponse[]>} List of DHCP leases. */ getDhcpLeases(serverName?: string): Promise<IRosGenericResponse[]>; /** * Updates a DHCP lease by ID with the provided parameters. * * @param {string} id - The internal ID of the DHCP lease (must start with '*'). * @param {Object} params - Lease parameters to update. * @param {string} [params.address] - The IP address to assign. * @param {string} [params.macAddress] - The MAC address of the device. * @param {string} [params.server] - The DHCP server name. * @param {string} [params.comment] - A comment for the lease. * @param {boolean} [params.disabled] - Whether to disable the lease. * @returns {Promise<IRosGenericResponse[]>} The response from the RouterOS API. * @throws {Error} If the ID is invalid or IP address is invalid. */ setDhcpLease(id: string, params: { address?: string; macAddress?: string; server?: string; comment?: string; disabled?: boolean; }): Promise<IRosGenericResponse[]>; /** * Updates a DHCP lease by ID and updates all firewall address-list entries * (of any list) from the old IP to the new IP (preserving lists, updating comment if given). * * @param {string} id - The internal ID of the DHCP lease (must start with '*'). * @param {Object} params - Lease parameters to update. * @param {string} [params.address] - The new IP address. * @param {string} [params.macAddress] - The MAC address. * @param {string} [params.server] - The DHCP server. * @param {string} [params.comment] - Optional comment for the lease/address-list. * @param {boolean} [params.disabled] - Disable the lease. * @returns {Promise<boolean>} The response from RouterOS API. */ setDhcpLeaseAndUpdateAddressLists(id: string, params: { address?: string; macAddress?: string; server?: string; comment?: string; disabled?: boolean; }): Promise<boolean>; /** * Remove a DHCP lease by ID and remove the associated IP from all firewall address-lists. * * @param {string} id - The internal ID of the DHCP lease (must start with '*'). * @returns {Promise<boolean>} True if the lease (and related firewall entries) were removed. * @throws {Error} If the ID is invalid or the lease is not found. */ removeDhcpLease(id: string): Promise<boolean>; /** * Fetches all DHCP servers along with IP addresses belonging to their interfaces. * * @returns {Promise<IRosGenericResponse[]>} */ getDhcpServersWithGateways(): Promise<IRosGenericResponse[]>; /** * Returns all free (unleased) IP addresses in a DHCP server's network, excluding the gateway itself. * * @param {string} serverName - The DHCP server name (e.g., "dhcp1"). * @param {string} gatewayCidr - The network in CIDR format (e.g., "192.168.1.1/24"). * @returns {Promise<string[]>} Array of free IP addresses as strings. * @throws {Error} If params are missing or invalid. */ getFreeIpsForDhcpServer(serverName: string, gatewayCidr: string): Promise<string[]>; /** * Finds a DHCP lease by its ID, gets its IP address, and adds that IP to the "Blocked" firewall address list. * Checks for duplicates and supports optional comment. * * @param {string} leaseId - The ID of the DHCP lease (must start with '*') * @param {string} [comment] - Optional comment for the address-list entry. * @returns {Promise<boolean>} True if the IP was added, false if already present. * @throws {Error} If the lease is not found or has no address. */ blockDhcpLeaseById(leaseId: string, comment?: string): Promise<boolean>; /** * Finds a DHCP lease by its ID, gets its IP address, and removes that IP from the "Blocked" firewall address list (if present). * * @param {string} leaseId - The ID of the DHCP lease (must start with '*') * @returns {Promise<boolean>} True if an address-list entry was removed, false if not present. * @throws {Error} If the lease is not found or has no address. */ unblockDhcpLeaseById(leaseId: string): Promise<boolean>; /** * Exports the current MikroTik configuration to a dated .rsc file on the device. * * @param {string} [filename] - Optional base filename (without date or extension). * @returns {Promise<string>} The actual filename used for the export (e.g., "backup_2025-06-10.rsc"). * @throws {Error} If writing to the router fails. */ exportConfig(filename?: string): Promise<string>; /** * Imports a MikroTik .rsc configuration file using the API. * * @param {string} filename - The filename to import (must end with .rsc and already exist on the device). * @returns {Promise<boolean>} True if the command was sent successfully. * @throws {Error} If the filename is missing or invalid. */ importConfig(filename: string): Promise<boolean>; /** * Get firewall rules */ getFirewallRules(chain?: string): Promise<IRosGenericResponse[]>; /** * Add a firewall rule */ addFirewallRule(chain: string, action: string, srcAddress?: string, dstAddress?: string, protocol?: string, port?: string): Promise<IRosGenericResponse[]>; /** * Get wireless interfaces */ getWirelessInterfaces(): Promise<IRosGenericResponse[]>; /** * Get wireless registration table */ getWirelessRegistrations(): Promise<IRosGenericResponse[]>; /** * Stream wireless registration table */ streamWirelessRegistrations(callback?: (err: Error, data: any) => void): RStream; /** * Get bridge interfaces */ getBridges(): Promise<IRosGenericResponse[]>; /** * Get routing table */ getRoutes(): Promise<IRosGenericResponse[]>; /** * Add a route */ addRoute(dstAddress: string, gateway: string, distance?: number): Promise<IRosGenericResponse[]>; /** * Get system logs */ getSystemLogs(topics?: string[]): Promise<IRosGenericResponse[]>; /** * Stream system logs */ streamSystemLogs(callback?: (err: Error, data: any) => void): RStream; /** * Reboot the device */ reboot(): Promise<IRosGenericResponse[]>; /** * Get system identity */ getSystemIdentity(): Promise<IRosGenericResponse[]>; /** * Set system identity */ setSystemIdentity(name: string): Promise<IRosGenericResponse[]>; /** * Get user list */ getUsers(): Promise<IRosGenericResponse[]>; /** * Add a user */ addUser(name: string, password: string, group?: string): Promise<IRosGenericResponse[]>; /** * Execute a script */ runScript(scriptName: string): Promise<IRosGenericResponse[]>; /** * Get interface statistics */ getInterfaceStats(interfaceName?: string): Promise<IRosGenericResponse[]>; /** * Stream interface statistics with customizable parameters */ streamInterfaceStats(params: { interface: string; interval?: number; duration?: number; }, callback?: (err: Error, data: any) => void): any; }