UNPKG

n8n-nodes-netdevices

Version:
255 lines 9.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CiscoConnection = void 0; const base_connection_1 = require("../base-connection"); class CiscoConnection extends base_connection_1.BaseConnection { constructor(credentials) { super(credentials); this.enablePassword = ''; this.inEnableMode = false; this.inConfigMode = false; this.enablePassword = credentials.enablePassword || credentials.password || ''; } async sessionPreparation() { await this.createCiscoShellChannel(); if (this.fastMode) { await this.setBasePrompt(); } else { await Promise.all([ this.setTerminalWidth(), this.disablePaging(), ]); await this.setBasePrompt(); await this.checkAndEnterEnableMode(); } } async createCiscoShellChannel() { 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 : 600; setTimeout(() => { resolve(); }, waitTime); }); }); } async setTerminalWidth() { try { await this.writeChannel('terminal width 511' + this.newline); await this.readChannel(2000); } catch (error) { } } async disablePaging() { try { await this.writeChannel('terminal length 0' + 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(); if (this.basePrompt.length > 16) { this.basePrompt = this.basePrompt.substring(0, 16); } this.enabledPrompt = this.basePrompt + '#'; this.configPrompt = this.basePrompt + '(config)#'; } async checkAndEnterEnableMode() { await this.writeChannel(this.returnChar); const output = await this.readChannel(2000); if (output.includes('#')) { this.inEnableMode = true; return; } await this.enterEnableMode(); } async enterEnableMode() { try { await this.writeChannel('enable' + this.newline); let output = await this.readChannel(3000); if (output.toLowerCase().includes('password:') || output.toLowerCase().includes('password')) { await this.writeChannel(this.enablePassword + this.newline); output = await this.readChannel(3000); } if (output.includes('#')) { this.inEnableMode = true; } else { throw new Error('Failed to enter enable mode'); } } catch (error) { throw new Error(`Failed to enter enable mode: ${error}`); } } async enterConfigMode() { if (!this.inEnableMode) { await this.enterEnableMode(); } 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 { 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'); } if (!this.fastMode) { if (!this.inEnableMode && !command.startsWith('show') && command !== 'enable') { await this.enterEnableMode(); } } 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(configCommands) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } await this.enterConfigMode(); let allOutput = ''; for (const command of configCommands) { await this.writeChannel(command + this.newline); const output = await this.readChannel(3000); allOutput += output; if (output.includes('Invalid input') || output.includes('% ')) { throw new Error(`Configuration error on command "${command}": ${output}`); } } await this.exitConfigMode(); const cleanOutput = this.sanitizeOutput(allOutput, configCommands.join('; ')); return { command: configCommands.join('; '), output: cleanOutput, success: true }; } catch (error) { if (this.inConfigMode) { try { await this.exitConfigMode(); } catch (exitError) { } } return { command: configCommands.join('; '), 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('write memory'); } async rebootDevice() { try { await this.writeChannel('reload' + this.newline); let output = await this.readChannel(5000); if (output.includes('[confirm]') || output.includes('?')) { await this.writeChannel(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 escapedCommand = command.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); let cleanOutput = output.replace(new RegExp(escapedCommand, 'g'), ''); const escapedBasePrompt = this.basePrompt.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); cleanOutput = cleanOutput.replace(new RegExp(escapedBasePrompt + '[>#$%]', 'g'), ''); cleanOutput = cleanOutput.replace(new RegExp(escapedBasePrompt + '\\(config\\)#', 'g'), ''); cleanOutput = cleanOutput.replace(/Building configuration\.\.\./g, ''); cleanOutput = cleanOutput.replace(/Current configuration : \d+ bytes/g, ''); cleanOutput = cleanOutput.replace(/!\s*$/gm, ''); cleanOutput = cleanOutput.replace(/^\s+|\s+$/g, ''); cleanOutput = cleanOutput.replace(/\r\n/g, '\n'); cleanOutput = cleanOutput.replace(/\r/g, '\n'); cleanOutput = cleanOutput.replace(/\n\s*\n/g, '\n'); return cleanOutput; } isInEnableMode() { return this.inEnableMode; } isInConfigMode() { return this.inConfigMode; } } exports.CiscoConnection = CiscoConnection; //# sourceMappingURL=cisco-connection.js.map