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)

194 lines 6.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HuaweiConnection = void 0; const base_connection_1 = require("../base-connection"); class HuaweiConnection extends base_connection_1.BaseConnection { constructor(credentials) { super(credentials); } async sessionPreparation() { await this.createHuaweiShellChannel(); await this.disablePaging(); await this.setTerminalWidth(); await this.setBasePrompt(); } async createHuaweiShellChannel() { return new Promise((resolve, reject) => { this.client.shell((err, channel) => { if (err) { reject(err); return; } this.currentChannel = channel; this.currentChannel.setEncoding(this.encoding); global.setTimeout(() => resolve(), 1000); }); }); } async disablePaging() { try { await this.writeChannel('screen-length 0 temporary' + this.newline); await this.readChannel(800); } catch { } } async setTerminalWidth() { try { await this.writeChannel('screen-width 300' + this.newline); await this.readChannel(600); } catch { } } async getCurrentConfig() { const cmd = 'display current-configuration'; try { const res = await this.sendCommand(cmd); return res; } catch (error) { return { command: cmd, output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } async saveConfig() { const cmd = 'save'; try { await this.writeChannel(cmd + this.newline); let output = await this.readChannel(2000); if (this.containsYesNoPrompt(output)) { await this.writeChannel('y' + this.newline); output += await this.readChannel(4000); } return { command: cmd, output: this.sanitizeOutput(output, cmd), success: true, }; } catch (error) { return { command: cmd, output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } async rebootDevice() { const cmd = 'reboot'; try { await this.writeChannel(cmd + this.newline); let output = await this.readChannel(3000); if (this.containsYesNoPrompt(output)) { await this.writeChannel('y' + this.newline); output += await this.readChannel(5000); } return { command: cmd, output: this.sanitizeOutput(output, cmd), success: true, }; } catch (error) { return { command: cmd, output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } async enterConfigMode() { await this.writeChannel('system-view' + this.newline); const out = await this.readChannel(800); try { await this.writeChannel('screen-length 0' + this.newline); await this.readChannel(400); } catch { } try { await this.writeChannel('screen-width 300' + this.newline); await this.readChannel(400); } catch { } if (!/\[.+\][#>\$]?/.test(out)) { } } async exitConfigMode() { await this.writeChannel('return' + this.newline); await this.readChannel(600); } async sendConfig(commands) { const joined = commands.join(' ; '); let output = ''; try { await this.enterConfigMode(); for (const cmd of commands) { await this.writeChannel(cmd + this.newline); const chunk = await this.readChannel(600); output += chunk; } await this.exitConfigMode(); return { command: `system-view ; ${joined} ; return`, output: this.sanitizeOutput(output, joined), success: true, }; } catch (error) { return { command: `system-view ; ${joined} ; return`, output: this.sanitizeOutput(output, joined), success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } sanitizeOutput(output, command) { let clean = output !== null && output !== void 0 ? output : ''; if (command) { const escaped = this.escapeRegex(command); clean = clean.replace(new RegExp(escaped, 'g'), ''); for (const part of command.split(/\s*;\s*/)) { if (!part) continue; clean = clean.replace(new RegExp(this.escapeRegex(part), 'g'), ''); } } if (this.basePrompt) { const bp = this.escapeRegex(this.basePrompt); clean = clean.replace(new RegExp(`<${bp}>[>#\\$%]?`, 'g'), ''); clean = clean.replace(new RegExp(`\\[${bp}\\][>#\\$%]?`, 'g'), ''); } else { clean = clean.replace(/<[^>]+>[>#\$\%]?/g, ''); clean = clean.replace(/\[[^\]]+\][>#\$\%]?/g, ''); } clean = clean.replace(/-+\s*More\s*-+/gi, ''); clean = clean.replace(/Press\s+ENTER\s+to\s+continue.*$/gim, ''); clean = clean.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); clean = clean.replace(/\n\s*\n\s*\n/g, '\n\n'); clean = clean.trim(); return clean; } containsYesNoPrompt(s) { if (!s) return false; return (/\[\s*Y\s*\/\s*N\s*\]\s*:?\s*$/i.test(s) || /Are\s+you\s+sure.*\[\s*Y\s*\/\s*N\s*\]\s*:?\s*$/i.test(s) || /Continue\?\s*\[\s*Y\s*\/\s*N\s*\]\s*:?\s*$/i.test(s)); } escapeRegex(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } } exports.HuaweiConnection = HuaweiConnection; //# sourceMappingURL=huawei-connection.js.map