-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibping.js
More file actions
100 lines (76 loc) · 2.23 KB
/
libping.js
File metadata and controls
100 lines (76 loc) · 2.23 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
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
/**
* Libreria per ping (stile NeDi - fping)
* Prima fa ping per trovare host attivi, poi fa SNMP solo su quelli
*/
class NetMapPing {
constructor() {
this.isWindows = process.platform === 'win32';
}
/**
* Ping singolo host
*/
async ping(ip, timeout = 1000) {
return new Promise((resolve) => {
const command = this.isWindows
? `ping -n 1 -w ${timeout} ${ip}`
: `ping -c 1 -W ${Math.floor(timeout / 1000)} ${ip}`;
exec(command, { timeout: timeout + 500 }, (error) => {
// Se error è null, ping ha avuto successo
resolve(!error);
});
});
}
/**
* Ping multipli in parallelo (stile fping)
*/
async pingMultiple(ips, concurrency = 10, timeout = 1000) {
const results = [];
const queue = [...ips];
const activeIPs = [];
const workers = new Array(concurrency).fill(null).map(async () => {
while (queue.length) {
const ip = queue.shift();
try {
const isAlive = await this.ping(ip, timeout);
if (isAlive) {
activeIPs.push(ip);
}
results.push({ ip, alive: isAlive });
} catch (err) {
results.push({ ip, alive: false });
}
}
});
await Promise.all(workers);
return { activeIPs, results };
}
/**
* Ping CIDR (stile NeDi fping -g)
* Restituisce solo gli IP attivi
*/
async pingCIDR(cidr, timeout = 1000, concurrency = 20) {
const Cidr = (await import('ip-cidr')).default;
if (!Cidr.isValidCIDR(cidr)) {
throw new Error('CIDR non valido');
}
const cidrObj = new Cidr(cidr);
const allIPs = cidrObj.toArray();
// Filtra network e broadcast
const ipParts = cidr.split('/');
const prefixLength = parseInt(ipParts[1]);
let hosts = allIPs;
if (prefixLength < 31) {
// Escludi network (primo) e broadcast (ultimo)
if (hosts.length > 2) {
hosts = hosts.slice(1, -1);
}
}
// Ping tutti gli host in parallelo
const { activeIPs } = await this.pingMultiple(hosts, concurrency, timeout);
return activeIPs;
}
}
export default NetMapPing;