-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathScan-the-LAN.js
64 lines (59 loc) · 1.74 KB
/
Scan-the-LAN.js
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
// 扫描局域网内的设备192.168.1.1-192.168.255.255
// 账号密码为root 1
// 端口号22
const { exec } = require('child_process');
const { Client } = require('ssh2');
const baseIP = '192.168'; // 修改为你的子网前缀
const startRange1 = 1;
const endRange1 = 255;
const startRange2 = 1;
const endRange2 = 255;
const username = 'root';
const password = '1';
async function scanNetwork() {
for (let i = startRange1; i <= endRange1; i++) {
for (let j = startRange2; j <= endRange2; j++) {
const ip = `${baseIP}.${i}.${j}`;
try {
await ping(ip);
console.log(`${ip} is alive`);
try {
await connectSSH(ip, username, password);
console.log(`Successfully connected to ${ip}`);
} catch (sshError) {
console.error(`Failed to connect to ${ip} via SSH:`, sshError.message);
}
} catch (pingError) {
console.error(`${ip} is not reachable`);
}
}
}
}
function ping(ip) {
return new Promise((resolve, reject) => {
exec(`ping -c 1 -W 1 ${ip}`, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
function connectSSH(ip, username, password) {
return new Promise((resolve, reject) => {
const conn = new Client();
conn.on('ready', () => {
conn.end();
resolve();
}).on('error', (err) => {
reject(err);
}).connect({
host: ip,
port: 22,
username: username,
password: password
});
});
}
scanNetwork();