ros-client
Version:
Node.js client for MikroTik RouterOS API with support for plain text and encrypted connections
248 lines (175 loc) โข 7.26 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. ๐
## ๐ What's New in v1.1.0
- ๐ **Comprehensive Command Reference** - Complete RouterOS API command reference in `Refrence.js`
- ๐งช **Production Example** - Enhanced `app.js` with advanced error handling and connectivity testing
- ๐ง **Improved Buffer Management** - Enhanced buffer handling for better performance
- ๐ **Enhanced Documentation** - Better examples and troubleshooting guides
- ๐ฌ **Community Support** - Telegram support group for community assistance
## โจ 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();
```
## ๐ Documentation & Examples
### ๐ Command Reference
For a comprehensive list of all available RouterOS API commands with detailed documentation, see the [**Refrence.js**](./Refrence.js) file. This file contains:
- ๐ง **System Commands** - Identity, resources, clock, logging, etc.
- ๐ **Interface Management** - Ethernet, wireless, bridge, VLAN, etc.
- ๐ **IP Configuration** - Addresses, routes, DNS, DHCP, firewall, etc.
- ๐ถ **Wireless Operations** - Registration, scanning, security, etc.
- ๐ **Security Features** - Firewall rules, NAT, user management, etc.
- ๐ **Monitoring Tools** - Traffic, queues, logs, statistics, etc.
### ๐งช Complete Example
For a production-ready example with advanced error handling and connectivity testing, see the [**app.js**](./app.js) file which demonstrates:
- โ
**Connection Testing** - TCP connectivity validation
- ๐ฅ **Advanced Error Handling** - Detailed error categorization and troubleshooting
- ๐ **Event Monitoring** - Comprehensive event listeners
- ๐จ **Timeout Management** - Connection and operation timeouts
- ๐ **Debug Logging** - Detailed connection and operation logging
## โ๏ธ 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
## ๐ฅ Contributors
We thank the following contributors for their valuable contributions to this project:
- **[mbingsdk](https://github.com/mbingsdk)** - Update limit buffer functionality ๐ง
- **[AviStudio](https://github.com/AviStudio)** - Command Reference (Refrence.js) documentation ๐
## ๐ฌ Support
Need help or have questions? Join our community:
๐ฒ **Telegram Support Group**: [https://t.me/ros_client](https://t.me/ros_client)
## ๐ Changelog
For detailed information about changes in each version, see the [CHANGELOG.md](./CHANGELOG.md) file.
---
โญ Star this repository if you find it helpful!