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)
159 lines • 5.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UbiquitiEdgeRouterConnection = void 0;
const base_connection_1 = require("../base-connection");
class UbiquitiEdgeRouterConnection extends base_connection_1.BaseConnection {
constructor(credentials) {
super(credentials);
this.inConfigMode = false;
}
async sessionPreparation() {
await this.createShellChannel();
if (this.fastMode) {
await this.setBasePrompt();
}
else {
await this.setBasePrompt();
await this.setEdgeRouterTerminalWidth();
await this.disableEdgeRouterPaging();
await new Promise(resolve => setTimeout(resolve, 300));
}
}
async setEdgeRouterTerminalWidth() {
try {
await this.writeChannel('terminal width 512' + this.newline);
await this.readChannel(2000);
}
catch (error) {
}
}
async disableEdgeRouterPaging() {
try {
await this.writeChannel('terminal length 0' + this.newline);
await this.readChannel(2000);
}
catch (error) {
}
}
async enterConfigMode() {
if (this.inConfigMode) {
return;
}
try {
await this.writeChannel('configure' + this.newline);
const output = await this.readChannel(3000);
if (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);
let output = await this.readChannel(3000);
if (output.includes('Cannot exit: configuration modified')) {
await this.writeChannel('exit discard' + this.newline);
output = await this.readChannel(3000);
}
if (!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 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') || output.includes('Error')) {
throw new Error(`Configuration error on command "${command}": ${output}`);
}
}
await this.writeChannel('commit' + this.newline);
const commitOutput = await this.readChannel(10000);
allOutput += commitOutput;
if (commitOutput.includes('Failed to generate committed config') ||
commitOutput.includes('Commit failed')) {
throw new Error(`Commit failed: ${commitOutput}`);
}
await this.exitConfigMode();
return {
command: configCommands.join('; '),
output: this.sanitizeOutput(allOutput, configCommands.join('; ')),
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 saveConfig() {
try {
if (!this.isConnected || !this.currentChannel) {
throw new Error('Not connected to device');
}
await this.writeChannel('save' + this.newline);
const output = await this.readChannel(5000);
if (!output.includes('Done')) {
throw new Error(`Save failed with following errors:\n\n${output}`);
}
return {
command: 'save',
output: this.sanitizeOutput(output, 'save'),
success: true
};
}
catch (error) {
return {
command: 'save',
output: '',
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
async getCurrentConfig() {
return await this.sendCommand('show configuration');
}
async showSystem() {
return await this.sendCommand('show system');
}
async showInterfaces() {
return await this.sendCommand('show interfaces');
}
}
exports.UbiquitiEdgeRouterConnection = UbiquitiEdgeRouterConnection;
//# sourceMappingURL=ubiquiti-edgerouter-connection.js.map