-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-mac-direct.mjs
More file actions
149 lines (121 loc) · 4.47 KB
/
query-mac-direct.mjs
File metadata and controls
149 lines (121 loc) · 4.47 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
#!/usr/bin/env node
/**
* Query SSH diretta con credenziali specifiche
*/
import { Client } from 'ssh2';
const switchIp = process.argv[2] || '192.168.1.1';
const macHuawei = process.argv[3] || '0000-0000-0001';
const username = process.env.SSH_USERNAME || 'admin';
const password = process.env.SWITCH_PASSWORD || 'changeme';
console.log('╔═══════════════════════════════════════════════════════════════════╗');
console.log('║ Query MAC via SSH ║');
console.log('╚═══════════════════════════════════════════════════════════════════╝\n');
console.log(`🔍 MAC Huawei format: ${macHuawei}`);
console.log(`📡 Switch: ${switchIp}`);
console.log(`👤 User: ${username}\n`);
const conn = new Client();
let output = '';
let commandSent = false;
const timeout = setTimeout(() => {
console.log('❌ Timeout connessione');
conn.end();
process.exit(1);
}, 25000);
conn.on('ready', () => {
console.log('✓ Connessione SSH stabilita\n');
conn.shell((err, stream) => {
if (err) {
clearTimeout(timeout);
console.error('❌ Errore shell:', err.message);
conn.end();
process.exit(1);
}
stream.on('close', () => {
clearTimeout(timeout);
conn.end();
// Analizza output
console.log('═══ Analisi Risultato ═══\n');
const lines = output.split('\n');
let inMacTable = false;
let macFound = false;
for (const line of lines) {
// Header tabella MAC
if (line.includes('MAC Address') && line.includes('VLAN')) {
inMacTable = true;
console.log(line);
continue;
}
// Separatore
if (inMacTable && line.includes('---')) {
console.log(line);
continue;
}
// Riga con MAC
if (inMacTable && line.includes('0000-481b-a4a7')) {
console.log(line);
macFound = true;
// Parse
const parts = line.trim().split(/\s+/);
if (parts.length >= 4) {
console.log('\n═══ RISULTATO FINALE ═══\n');
console.log(` ✅ MAC: ${parts[0]}`);
console.log(` ✅ VLAN: ${parts[1]}`);
console.log(` ✅ PORTA: ${parts[parts.length - 1]}`);
console.log(` 📍 SWITCH: ${switchIp}`);
}
}
// Fine tabella
if (inMacTable && line.includes('Total')) {
console.log(line);
break;
}
}
if (!macFound) {
console.log('⚠ MAC non trovato nella tabella');
console.log('\nOutput raw:');
console.log(output.substring(output.indexOf('display mac-address')));
}
console.log('\n═══════════════════════════════════════════════════════════════════\n');
});
stream.on('data', (data) => {
const text = data.toString();
output += text;
// Invia comandi al primo prompt
if (!commandSent && (text.includes('>') || text.includes(']'))) {
commandSent = true;
console.log('⚡ Eseguo comandi...\n');
// Disabilita paging
stream.write('screen-length 0 temporary\n');
setTimeout(() => {
stream.write(`display mac-address ${macHuawei}\n`);
setTimeout(() => {
stream.write('quit\n');
}, 2000);
}, 500);
}
});
});
});
conn.on('error', (err) => {
clearTimeout(timeout);
console.error('❌ Errore SSH:', err.message);
if (err.message.includes('auth') || err.message.includes('Auth')) {
console.log('\n Credenziali non valide. Prova manualmente:');
console.log(` ssh admin@${switchIp}`);
console.log(` display mac-address ${macHuawei}`);
}
process.exit(1);
});
console.log('Connessione in corso...\n');
conn.connect({
host: switchIp,
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']
}
});