-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-mac-ssh.mjs
More file actions
190 lines (159 loc) · 5.58 KB
/
query-mac-ssh.mjs
File metadata and controls
190 lines (159 loc) · 5.58 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
#!/usr/bin/env node
/**
* Query SSH live per trovare porta esatta di un MAC address
*/
import { readFileSync } from 'fs';
import { Client } from 'ssh2';
const switchIp = process.argv[2] || '192.168.7.171';
const macInput = process.argv[3] || '00:00:48:1b:a4:a7';
// Normalizza MAC in formato Huawei (xxxx-xxxx-xxxx)
function formatMacHuawei(mac) {
const clean = mac.replace(/[^0-9a-fA-F]/g, '').toLowerCase();
if (clean.length !== 12) return null;
return clean.match(/.{4}/g).join('-');
}
const macHuawei = formatMacHuawei(macInput);
console.log('╔═══════════════════════════════════════════════════════════════════╗');
console.log('║ NetMap - Query SSH Live MAC Address ║');
console.log('╚═══════════════════════════════════════════════════════════════════╝\n');
console.log(`🔍 MAC: ${macInput}`);
console.log(` Formato Huawei: ${macHuawei}`);
console.log(` Switch: ${switchIp}\n`);
// Leggi credenziali
const csvPath = './Pdv.CSV';
let credentials = [];
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.length >= 3 && parts[1] && parts[2]) {
credentials.push({
site: parts[0],
user: parts[1],
pass: parts[2]
});
}
}
console.log(`✓ Caricate ${credentials.length} credenziali\n`);
} catch (err) {
console.error('❌ Errore lettura credenziali:', err.message);
process.exit(1);
}
// Funzione per eseguire comando SSH
function execSSH(host, username, password, command) {
return new Promise((resolve, reject) => {
const conn = new Client();
let output = '';
let shellReady = false;
const timeout = setTimeout(() => {
conn.end();
reject(new Error('Timeout connessione'));
}, 30000);
conn.on('ready', () => {
console.log(` ✓ Connesso con ${username}`);
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;
// Attendi prompt prima di inviare comando
if (!shellReady && (text.includes('>') || text.includes('#') || text.includes(']'))) {
shellReady = true;
// Disabilita paginazione
stream.write('screen-length 0 temporary\n');
setTimeout(() => {
stream.write(command + '\n');
setTimeout(() => {
stream.write('quit\n');
}, 3000);
}, 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']
}
});
});
}
// Prova le credenziali
console.log('═══ Connessione SSH ═══\n');
let connected = false;
let result = null;
for (const cred of credentials) {
if (connected) break;
console.log(` Provo: ${cred.user}...`);
try {
const command = `display mac-address ${macHuawei}`;
result = await execSSH(switchIp, cred.user, cred.pass, command);
connected = true;
console.log(`\n═══ Risultato ═══\n`);
// Estrai solo la parte rilevante dell'output
const lines = result.split('\n');
let inTable = false;
let relevantLines = [];
for (const line of lines) {
// Cerca header tabella MAC
if (line.includes('MAC Address') && line.includes('VLAN')) {
inTable = true;
}
if (inTable) {
relevantLines.push(line);
}
// Fine tabella
if (inTable && line.includes('Total matching')) {
break;
}
}
if (relevantLines.length > 0) {
console.log(relevantLines.join('\n'));
} else {
// Mostra output raw se non parsato
console.log(result);
}
// Parse interfaccia
const ifMatch = result.match(/(GE|XGE|Eth-Trunk|GigabitEthernet|XGigabitEthernet)[^\s]*\d+\/\d+\/\d+/gi);
const vlanMatch = result.match(/(\d+)\s+[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}/i);
console.log('\n═══ Riepilogo ═══\n');
if (ifMatch) {
console.log(` ✅ PORTA: ${ifMatch[0]}`);
}
if (vlanMatch) {
console.log(` ✅ VLAN: ${vlanMatch[1]}`);
}
console.log(` ✅ SWITCH: ${switchIp}`);
} catch (err) {
// Prova prossima credenziale
if (err.message.includes('Authentication') || err.message.includes('auth')) {
continue;
}
console.log(` ⚠ Errore: ${err.message}`);
}
}
if (!connected) {
console.log('\n❌ Impossibile connettersi allo switch con le credenziali disponibili');
}
console.log('\n═══════════════════════════════════════════════════════════════════\n');