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)
184 lines • 6.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AristaConnection = void 0;
const cisco_connection_1 = require("../cisco/cisco-connection");
class AristaConnection extends cisco_connection_1.CiscoConnection {
constructor(credentials) {
super(credentials);
this.promptPattern = /[$>#]/;
}
async sessionPreparation() {
await this.createCiscoShellChannel();
if (this.fastMode) {
await this.setBasePrompt();
}
else {
await this.setAristaTerminalWidth();
await this.disableAristaPaging();
await this.setBasePrompt();
await this.checkAndEnterEnableMode();
}
}
async setAristaTerminalWidth() {
try {
await this.writeChannel('terminal width 511' + this.newline);
const output = await this.readChannel(2000);
if (!output.includes('Width set to')) {
}
}
catch (error) {
}
}
async disableAristaPaging() {
try {
await this.writeChannel('terminal length 0' + this.newline);
const output = await this.readChannel(2000);
if (!output.includes('Pagination disabled')) {
}
}
catch (error) {
}
}
async checkConfigMode() {
try {
await this.writeChannel(this.returnChar);
let output = await this.readChannel(2000);
output = output.replace(/\(s1\)/g, '');
output = output.replace(/\(s2\)/g, '');
return output.includes('(config)#');
}
catch (error) {
return false;
}
}
async enterConfigMode() {
if (await this.checkConfigMode()) {
this.inConfigMode = true;
return;
}
if (!this.inEnableMode) {
await this.enterEnableMode();
}
try {
await this.writeChannel('configure terminal' + this.newline);
const output = await this.readChannel(3000);
const cleanOutput = output.replace(/\(s1\)/g, '').replace(/\(s2\)/g, '');
if (cleanOutput.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 findPrompt() {
try {
await this.writeChannel(this.returnChar);
const output = await this.readChannel(2000);
const lines = output.trim().split('\n');
const lastLine = lines[lines.length - 1].trim();
return lastLine;
}
catch (error) {
return '';
}
}
async enterBashShell() {
try {
if (!this.isConnected || !this.currentChannel) {
throw new Error('Not connected to device');
}
await this.writeChannel('bash' + this.newline);
const output = await this.readChannel(3000);
return {
command: 'bash',
output: output,
success: true
};
}
catch (error) {
return {
command: 'bash',
output: '',
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
async exitBashShell() {
try {
if (!this.isConnected || !this.currentChannel) {
throw new Error('Not connected to device');
}
await this.writeChannel('exit' + this.newline);
const output = await this.readChannel(3000);
return {
command: 'exit',
output: output,
success: true
};
}
catch (error) {
return {
command: 'exit',
output: '',
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
async getVersionJson() {
return await this.sendCommand('show version | json');
}
async getCurrentConfig() {
return await this.sendCommand('show running-config');
}
async saveConfig() {
return await this.sendCommand('write memory');
}
async rebootDevice() {
try {
await this.writeChannel('reload now' + this.newline);
const output = await this.readChannel(5000);
return {
command: 'reload now',
output: output,
success: true
};
}
catch (error) {
return {
command: 'reload now',
output: '',
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
}
sanitizeOutput(output, command) {
const lines = output.split('\n');
let cleanLines = lines.filter((line, index) => {
if (index > 0 && line.trim() === command.trim() && lines[index - 1].trim() === command.trim()) {
return false;
}
return true;
});
if (cleanLines.length > 0 && cleanLines[0].includes(command)) {
cleanLines.shift();
}
if (cleanLines.length > 0) {
const lastLine = cleanLines[cleanLines.length - 1];
const cleanLastLine = lastLine.replace(/\(s1\)/g, '').replace(/\(s2\)/g, '');
const promptRegex = new RegExp(`^${this.escapeRegex(this.basePrompt)}[>#$]`);
if (promptRegex.test(cleanLastLine)) {
cleanLines.pop();
}
}
return cleanLines.join('\n').trim();
}
}
exports.AristaConnection = AristaConnection;
//# sourceMappingURL=arista-connection.js.map