UNPKG

n8n-nodes-netdevices

Version:

n8n node to interact with network devices (Cisco, MikroTik, Arista, Extreme Networks, Dell, Juniper, HP, Aruba, Ubiquiti, Palo Alto, Fortinet - ISP, data center, campus, and edge)

173 lines 6.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MikrotikSwitchOsConnection = exports.MikrotikRouterOsConnection = exports.MikrotikConnection = void 0; const base_connection_1 = require("../base-connection"); class MikrotikConnection extends base_connection_1.BaseConnection { constructor(credentials) { super(credentials); this.promptPattern = /\].*>/; this.originalUsername = credentials.username; this.newline = '\r\n'; } modifyUsername() { return `${this.originalUsername}+ct511w4098h`; } async connect() { this.credentials.username = this.modifyUsername(); await super.connect(); this.credentials.username = this.originalUsername; } async sessionPreparation() { await this.handleSpecialLogin(); await this.setBasePrompt(); } async handleSpecialLogin() { try { await new Promise(resolve => setTimeout(resolve, 1000)); const output = await this.readChannel(2000); if (output.includes('Please press "Enter" to continue')) { await this.writeChannel(this.returnChar); await this.readChannel(2000); } else if (output.toLowerCase().includes('do you want to see the software license')) { await this.writeChannel('n' + this.newline); await this.readChannel(2000); } } catch (error) { } } async setBasePrompt() { await this.writeChannel(this.returnChar); const output = await this.readChannel(3000); const lines = output.trim().split('\n'); const lastLine = lines[lines.length - 1].trim(); this.basePrompt = lastLine.replace(/>\s*$/, '').trim(); } async sendCommand(command) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } await this.writeChannel(command + this.newline); const output = await this.readUntilPrompt(undefined, 10000); const cleanOutput = this.sanitizeMikrotikOutput(output, command); return { command, output: cleanOutput, success: true }; } catch (error) { return { command, output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } async sendConfig(configCommands) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } let allOutput = ''; for (const command of configCommands) { const result = await this.sendCommand(command); if (!result.success) { throw new Error(`Command failed: ${command} - ${result.error}`); } allOutput += result.output + '\n'; } return { command: configCommands.join('; '), output: allOutput.trim(), success: true }; } catch (error) { return { command: configCommands.join('; '), output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } async getCurrentConfig() { return await this.sendCommand('/export'); } async getSystemInfo() { return await this.sendCommand('/system resource print'); } async getRouterBoard() { return await this.sendCommand('/system routerboard print'); } async saveConfig() { const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); return await this.sendCommand(`/system backup save name=backup-${timestamp}`); } async rebootDevice() { try { await this.writeChannel('/system reboot' + this.newline); let output = await this.readChannel(3000); if (output.toLowerCase().includes('do you really want to reboot')) { await this.writeChannel('y' + this.newline); output += await this.readChannel(3000); } return { command: '/system reboot', output: output, success: true }; } catch (error) { return { command: '/system reboot', output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } sanitizeMikrotikOutput(output, command) { let lines = output.split('\n'); lines = lines.filter(line => { const trimmed = line.trim(); if (trimmed.includes('] >') && trimmed.includes(command)) { return false; } return true; }); if (lines.length > 0) { const lastLine = lines[lines.length - 1]; if (lastLine.includes('] >') || this.promptPattern.test(lastLine)) { lines.pop(); } } return lines.join('\n').trim(); } async disconnect() { if (!this.isConnected) { return; } try { await this.writeChannel('quit' + this.newline); await this.readChannel(1000); } catch (error) { } finally { await super.disconnect(); } } } exports.MikrotikConnection = MikrotikConnection; class MikrotikRouterOsConnection extends MikrotikConnection { } exports.MikrotikRouterOsConnection = MikrotikRouterOsConnection; class MikrotikSwitchOsConnection extends MikrotikConnection { } exports.MikrotikSwitchOsConnection = MikrotikSwitchOsConnection; //# sourceMappingURL=mikrotik-connection.js.map