ros-client
Version:
Node.js client for MikroTik RouterOS API with support for plain text and encrypted connections
200 lines (144 loc) ⢠4.98 kB
Markdown
# RouterOS API Client š
[](https://www.npmjs.org/package/ros-client)
[](https://travis-ci.org/SDWORLLD/ros-client)
[](https://opensource.org/licenses/MIT)
A powerful Node.js module for seamless interaction with MikroTik RouterOS API, supporting both plain text (port 8728) and encrypted (port 8729) connections. š
## ⨠Key Features
- š Secure connections via plain text and TLS encryption
- š Full support for RouterOS API protocol encoding/decoding
- ā” Modern Promise-based API implementation
- š ļø Comprehensive MikroTik API command helpers
- š Robust error handling mechanism
- š Built-in debug mode for troubleshooting
- š¦ Lightweight with minimal dependencies
## š¦ Installation
```bash
npm install ros-client
```
## š Quick Start
```javascript
const RouterOSClient = require("ros-client");
// Create API client instance
const api = new RouterOSClient({
host: "192.168.88.1",
username: "admin",
password: "your-password",
port: 8728, // Use 8729 for TLS
tls: false, // Set to true for encrypted connection
});
async function example() {
try {
await api.connect();
console.log("ā
Connected successfully!");
// Get system identity
const identity = await api.send(["/system/identity/print"]);
console.log("š„ļø Router identity:", identity);
// Get all interfaces
const interfaces = await api.send(["/interface/print"]);
console.log("š Interfaces:", interfaces);
await api.close();
} catch (err) {
console.error("ā Error:", err.message);
}
}
example();
```
## āļø Configuration Options
```javascript
const api = new RouterOSClient({
host: "192.168.88.1", // Router IP address
username: "admin", // Username
password: "password", // Password
port: 8728, // API port (8729 for TLS)
tls: false, // TLS encryption
timeout: 10000, // Connection timeout (ms)
debug: false, // Debug output
});
```
## š API Documentation
### š Connection Methods
#### `connect()`
Establishes and authenticates the RouterOS connection.
```javascript
await api.connect();
```
#### `close()`
Gracefully terminates the RouterOS connection.
```javascript
await api.close();
```
### š ļø Command Methods
#### `send(words)`
Executes commands on the RouterOS device.
```javascript
const result = await api.send(["/system/resource/print"]);
```
## š Command Examples
### š„ļø System Management
```javascript
// System identity operations
const identity = await api.send(["/system/identity/print"]);
await api.send(["/system/identity/set", "=name=my-router"]);
// Resource monitoring
const resources = await api.send(["/system/resource/print"]);
// System maintenance
await api.send(["/system/reboot"]);
```
### š Network Interface Management
```javascript
// Interface operations
const interfaces = await api.send(["/interface/print"]);
await api.send(["/interface/enable", "=.id=ether1"]);
// Wireless interface management
const wireless = await api.send(["/interface/wireless/print"]);
```
### š§ IP Configuration
```javascript
// IP address management
const addresses = await api.send(["/ip/address/print"]);
await api.send([
"/ip/address/add",
"=address=192.168.1.1/24",
"=interface=ether1",
]);
// DHCP server management
const leases = await api.send(["/ip/dhcp-server/lease/print"]);
```
## šÆ Events
The client emits these events:
- `connected` ā
- Connection established
- `error` ā - Error occurred
- `close` š - Connection terminated
```javascript
api.on("connected", () => console.log("ā
Connected"));
api.on("error", (err) => console.error("ā Error:", err.message));
api.on("close", () => console.log("š Connection closed"));
```
## š Debug Mode
Enable detailed logging:
```javascript
const api = new RouterOSClient({
debug: true,
// other options...
});
```
## ā Error Handling
```javascript
try {
await api.connect();
const result = await api.send(["/command"]);
} catch (err) {
console.error("ā Error:", err.message);
}
```
## š License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## š¤ Contributing
We welcome contributions! Here's how:
1. š Fork the repository
2. šæ Create your feature branch (`git checkout -b feature/amazing-feature`)
3. āļø Commit changes (`git commit -m 'Add amazing feature'`)
4. š¤ Push to branch (`git push origin feature/amazing-feature`)
5. š« Open a Pull Request
---
ā Star this repository if you find it helpful!