-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-ethtrunk.mjs
More file actions
206 lines (176 loc) · 6.61 KB
/
debug-ethtrunk.mjs
File metadata and controls
206 lines (176 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
/**
* Debug Eth-Trunk trace per MAC 00:00:48:32:51:e2
*/
import { readFileSync } from 'fs';
import { Client } from 'ssh2';
const switchIp = '192.168.7.251'; // Core switch dove MAC è su Eth-Trunk78
const macHuawei = '0000-4832-51e2';
const trunkNum = 78;
// Leggi credenziali sito 7 (Seriate)
const csvPath = './Pdv.CSV';
let sshCreds = { user: process.env.SSH_USERNAME || 'admin', pass: process.env.SWITCH_PASSWORD || 'changeme' };
try {
const csv = readFileSync(csvPath, 'utf-8');
const lines = csv.split(/\r?\n/).filter(l => l.trim());
for (const line of lines) {
const parts = line.split(';');
if (parts[0] === '7' && parts[3] && parts[4]) {
sshCreds = { user: parts[3], pass: parts[4] };
break;
}
}
} catch (e) {
console.log('⚠ Usando credenziali default');
}
console.log('╔═══════════════════════════════════════════════════════════════════╗');
console.log('║ Debug Eth-Trunk Trace ║');
console.log('╚═══════════════════════════════════════════════════════════════════╝\n');
console.log(`📍 Switch: ${switchIp}`);
console.log(`🔍 MAC: ${macHuawei}`);
console.log(`🔗 Eth-Trunk: ${trunkNum}`);
console.log(`👤 User: ${sshCreds.user}\n`);
// Parser membri trunk
function parseTrunkMembers(text) {
const lines = String(text || '').split(/\r?\n/);
const members = [];
for (const line of lines) {
const m = line.match(/(?:Gigabit|XGigabit|GE|Ethernet)[A-Za-z]*\d+\/\d+\/\d+/i);
if (m) members.push(m[0]);
}
return Array.from(new Set(members));
}
// Parser LLDP neighbor
function parseLldpNeighborIp(text) {
const lines = String(text || '').split(/\r?\n/);
for (const line of lines) {
const m = line.match(/Management\s+address[:\s]+(\d+\.\d+\.\d+\.\d+)/i);
if (m) return m[1];
}
return null;
}
function parseLldpNeighborName(text) {
const lines = String(text || '').split(/\r?\n/);
for (const line of lines) {
const m = line.match(/System\s+name[:\s]+(\S+)/i);
if (m) return m[1];
}
return null;
}
// Esegue comando SSH
function execSSH(host, username, password, command) {
return new Promise((resolve, reject) => {
const conn = new Client();
let output = '';
let commandSent = false;
const timeout = setTimeout(() => {
conn.end();
reject(new Error('Timeout'));
}, 25000);
conn.on('ready', () => {
conn.shell((err, stream) => {
if (err) {
clearTimeout(timeout);
conn.end();
return reject(err);
}
stream.on('close', () => {
clearTimeout(timeout);
conn.end();
resolve(output);
});
stream.on('data', (data) => {
const text = data.toString();
output += text;
if (!commandSent && (text.includes('>') || text.includes('#') || text.includes(']'))) {
commandSent = true;
stream.write('screen-length 0 temporary\n');
setTimeout(() => {
stream.write(command + '\n');
setTimeout(() => {
stream.write('quit\n');
}, 2000);
}, 500);
}
});
});
});
conn.on('error', (err) => {
clearTimeout(timeout);
reject(err);
});
conn.connect({
host,
port: 22,
username,
password,
readyTimeout: 10000,
algorithms: {
kex: ['diffie-hellman-group14-sha1', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group1-sha1'],
cipher: ['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', '3des-cbc'],
hmac: ['hmac-sha1', 'hmac-sha2-256']
}
});
});
}
async function main() {
try {
// Step 1: Conferma MAC su Eth-Trunk
console.log('═══ Step 1: Verifica MAC su Eth-Trunk ═══\n');
const macOut = await execSSH(switchIp, sshCreds.user, sshCreds.pass, `display mac-address ${macHuawei}`);
// Estrai info
const macLines = macOut.split('\n');
for (const line of macLines) {
if (line.includes(macHuawei) || line.includes('Eth-Trunk')) {
console.log(' ' + line.trim());
}
}
// Step 2: Get trunk members
console.log('\n═══ Step 2: Membri Eth-Trunk ═══\n');
const trunkOut = await execSSH(switchIp, sshCreds.user, sshCreds.pass, `display eth-trunk ${trunkNum}`);
console.log('Raw output (primi 50 righe):');
const trunkLines = trunkOut.split('\n').slice(0, 50);
for (const line of trunkLines) {
if (line.trim()) console.log(' ' + line);
}
const members = parseTrunkMembers(trunkOut);
console.log(`\n✓ Membri parsati: ${members.length}`);
members.forEach(m => console.log(` - ${m}`));
if (members.length === 0) {
console.log('\n⚠ PROBLEMA: Nessun membro trovato!');
console.log(' Il parser potrebbe non riconoscere il formato.');
return;
}
// Step 3: LLDP su ogni membro
console.log('\n═══ Step 3: LLDP Neighbors sui membri ═══\n');
for (const member of members) {
console.log(`\n--- ${member} ---`);
const lldpOut = await execSSH(switchIp, sshCreds.user, sshCreds.pass, `display lldp neighbor interface ${member}`);
const neighborIp = parseLldpNeighborIp(lldpOut);
const neighborName = parseLldpNeighborName(lldpOut);
if (neighborIp || neighborName) {
console.log(` ✅ Neighbor trovato:`);
if (neighborName) console.log(` Name: ${neighborName}`);
if (neighborIp) console.log(` IP: ${neighborIp}`);
} else {
console.log(` ⚠ Nessun neighbor LLDP`);
// Mostra output raw per debug
const lldpLines = lldpOut.split('\n').filter(l => l.trim());
if (lldpLines.length > 3) {
console.log(' Raw (primi 10 righe):');
lldpLines.slice(0, 10).forEach(l => console.log(' ' + l.trim()));
}
}
}
console.log('\n═══ Analisi ═══\n');
console.log('Se nessun neighbor LLDP è trovato sui membri del trunk,');
console.log('il trace non può continuare perché non sa dove andare.');
console.log('\nPossibili cause:');
console.log('1. Il neighbor è un device che non supporta LLDP');
console.log('2. LLDP è disabilitato sul neighbor');
console.log('3. Il trunk porta a un device non gestito (es. AP, server)');
} catch (err) {
console.error('❌ Errore:', err.message);
}
}
main();