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)

296 lines 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PaloAltoConnection = void 0; const base_connection_1 = require("../base-connection"); class PaloAltoConnection extends base_connection_1.BaseConnection { constructor(credentials) { super(credentials); this.inConfigMode = false; } async sessionPreparation() { await this.createPaloAltoShellChannel(); if (this.fastMode) { await this.setBasePrompt(); } else { await Promise.all([ this.setTerminalWidth(), this.disablePaging(), this.setScriptingMode(), ]); await this.setBasePrompt(); await this.writeChannel('show system info' + this.newline); await this.readChannel(3000); await this.readUntilPrompt(undefined, 5000); } } async createPaloAltoShellChannel() { 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('set cli terminal width 500' + this.newline); await this.readChannel(2000); } catch (error) { } } async disablePaging() { try { await this.writeChannel('set cli pager off' + this.newline); await this.readChannel(2000); } catch (error) { } } async setScriptingMode() { try { await this.writeChannel('set cli scripting-mode on' + 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 + '#'; } async enterConfigMode() { try { await this.writeChannel('configure' + this.newline); const output = await this.readChannel(3000); if (output.includes('#') || output.includes('[edit')) { 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('#') && !output.includes('[edit')) { 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 ? 8000 : 15000; 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 : String(error) }; } } async sendConfig(configCommands) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } await this.enterConfigMode(); let fullOutput = ''; for (const command of configCommands) { await this.writeChannel(command + this.newline); const output = await this.readChannel(3000); fullOutput += output; } await this.exitConfigMode(); const cleanOutput = this.sanitizeOutput(fullOutput, configCommands.join('\n')); return { command: configCommands.join('\n'), output: cleanOutput, success: true }; } catch (error) { try { if (this.inConfigMode) { await this.exitConfigMode(); } } catch (exitError) { } return { command: configCommands.join('\n'), output: '', success: false, error: error instanceof Error ? error.message : String(error) }; } } async commit(comment = '', force = false, partial = false, deviceAndNetwork = false, policyAndObjects = false, vsys = '', noVsys = false) { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } if ((deviceAndNetwork || policyAndObjects || vsys || noVsys) && !partial) { throw new Error("'partial' must be True when using deviceAndNetwork or policyAndObjects or vsys or noVsys."); } await this.enterConfigMode(); let commandString = 'commit'; const commitMarker = 'configuration committed successfully'; if (comment) { commandString += ` description "${comment}"`; } if (force) { commandString += ' force'; } if (partial) { commandString += ' partial'; if (vsys) { commandString += ` ${vsys}`; } if (deviceAndNetwork) { commandString += ' device-and-network'; } if (policyAndObjects) { commandString += ' policy-and-objects'; } if (noVsys) { commandString += ' no-vsys'; } commandString += ' excluded'; } await this.writeChannel(commandString + this.newline); const output = await this.readUntilPrompt(undefined, 120000); await this.exitConfigMode(); if (!output.toLowerCase().includes(commitMarker)) { throw new Error(`Commit failed with the following errors:\n\n${output}`); } return { command: commandString, output: this.sanitizeOutput(output, commandString), success: true }; } catch (error) { try { if (this.inConfigMode) { await this.exitConfigMode(); } } catch (exitError) { } return { command: 'commit', output: '', success: false, error: error instanceof Error ? error.message : String(error) }; } } async getCurrentConfig() { return this.sendCommand('show config running'); } async saveConfig() { return this.sendCommand('show config saved'); } async rebootDevice() { try { if (!this.isConnected || !this.currentChannel) { throw new Error('Not connected to device'); } await this.writeChannel('request restart system' + this.newline); const output = await this.readChannel(5000); if (output.toLowerCase().includes('yes/no') || output.toLowerCase().includes('y/n')) { await this.writeChannel('yes' + this.newline); await this.readChannel(3000); } return { command: 'request restart system', output: this.sanitizeOutput(output, 'request restart system'), success: true }; } catch (error) { return { command: 'request restart system', output: '', success: false, error: error instanceof Error ? error.message : String(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; } async cleanup() { try { if (this.inConfigMode) { await this.exitConfigMode(); } } catch (error) { } try { await this.writeChannel('exit' + this.newline); } catch (error) { } await this.disconnect(); } } exports.PaloAltoConnection = PaloAltoConnection; //# sourceMappingURL=paloalto-connection.js.map