UNPKG

n8n-nodes-netdevices

Version:
228 lines 8.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CiscoSG300Connection = void 0; const base_connection_1 = require("../base-connection"); class CiscoSG300Connection extends base_connection_1.BaseConnection { constructor(credentials) { super(credentials); this.inConfigMode = false; this.inEnableMode = false; this.enablePassword = ''; this.enablePassword = credentials.enablePassword || credentials.password || ''; } async sessionPreparation() { await this.createSG300ShellChannel(); await this.setTerminalSettings(); await this.setBasePrompt(); await this.checkAndEnterEnableMode(); } async createSG300ShellChannel() { 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 setTerminalSettings() { try { await this.writeChannel('terminal datadump' + 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.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' + 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(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.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('copy running-config startup-config'); } async rebootDevice() { try { await this.writeChannel('reload' + this.newline); let output = await this.readChannel(5000); if (output.includes('confirm') || output.includes('[Y/N]')) { await this.writeChannel('y' + 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(/^\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.CiscoSG300Connection = CiscoSG300Connection; //# sourceMappingURL=cisco-sg300-connection.js.map