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)

199 lines 6.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CiscoIOSXRConnection = void 0; const base_connection_1 = require("../base-connection"); class CiscoIOSXRConnection extends base_connection_1.BaseConnection { constructor(credentials) { super(credentials); this.inConfigMode = false; } async sessionPreparation() { await this.createIOSXRShellChannel(); await this.setTerminalSettings(); await this.setBasePrompt(); } async createIOSXRShellChannel() { return new Promise((resolve, reject) => { this.client.shell((err, channel) => { if (err) { reject(err); return; } this.currentChannel = channel; this.currentChannel.setEncoding(this.encoding); const waitTime = this.fastMode ? 200 : 500; setTimeout(() => { resolve(); }, waitTime); }); }); } async setTerminalSettings() { try { await this.writeChannel('terminal length 0' + this.newline); await this.readChannel(2000); await this.writeChannel('terminal width 511' + 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]; this.basePrompt = lastLine.replace(/[>#$%]\s*$/, '').trim(); this.configPrompt = this.basePrompt + '(config)#'; } async enterConfigMode() { try { await this.writeChannel('configure terminal' + this.newline); const output = await this.readChannel(3000); if (output.includes('(config)#')) { this.inConfigMode = true; } else { throw new Error('Failed to enter configuration mode'); } } catch (error) { throw new Error(`Failed to enter configuration mode: ${error}`); } } async exitConfigMode() { if (!this.inConfigMode) { return; } try { await this.writeChannel('exit' + this.newline); const output = await this.readChannel(3000); if (output.includes('#') && !output.includes('(config)#')) { this.inConfigMode = false; } else { await this.writeChannel('end' + this.newline); const endOutput = await this.readChannel(3000); if (endOutput.includes('#') && !endOutput.includes('(config)#')) { this.inConfigMode = false; } else { throw new Error('Failed to exit configuration mode'); } } } catch (error) { throw new Error(`Failed to exit configuration mode: ${error}`); } } async sendCommand(command) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } await this.writeChannel(command + this.newline); const timeout = this.fastMode ? 5000 : 10000; const output = await this.readUntilPrompt(undefined, timeout); const cleanOutput = this.sanitizeOutput(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(commands) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } let fullOutput = ''; let allCommands = commands.join('\n'); await this.enterConfigMode(); for (const command of commands) { await this.writeChannel(command + this.newline); const output = await this.readChannel(3000); fullOutput += output; } await this.writeChannel('commit' + this.newline); const commitOutput = await this.readChannel(5000); fullOutput += commitOutput; await this.exitConfigMode(); const cleanOutput = this.sanitizeOutput(fullOutput, allCommands); return { command: allCommands, output: cleanOutput, success: true }; } catch (error) { try { await this.exitConfigMode(); } catch (exitError) { } return { command: commands.join('\n'), output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } async getCurrentConfig() { return await this.sendCommand('show running-config'); } async saveConfig() { return await this.sendCommand('show configuration commit changes last 1'); } async rebootDevice() { try { await this.writeChannel('reload' + this.newline); let output = await this.readChannel(5000); if (output.includes('confirm') || output.includes('Proceed')) { await this.writeChannel('yes' + this.newline); output += await this.readChannel(5000); } return { command: 'reload', output: output, success: true }; } catch (error) { return { command: 'reload', output: '', success: false, error: error instanceof Error ? error.message : 'Unknown error' }; } } sanitizeOutput(output, command) { const lines = output.split('\n'); if (lines.length > 0 && lines[0].includes(command)) { lines.shift(); } if (lines.length > 0) { const lastLine = lines[lines.length - 1]; const promptRegex = new RegExp(`^${this.escapeRegex(this.basePrompt)}[>#$]`); if (promptRegex.test(lastLine)) { lines.pop(); } } return lines.join('\n').trim(); } isInConfigMode() { return this.inConfigMode; } } exports.CiscoIOSXRConnection = CiscoIOSXRConnection; //# sourceMappingURL=cisco-ios-xr-connection.js.map