比如你的手机开了ftp,或者别的电脑开了什么服务,你懒得再过去翻看ip地址,就用这个脚本,自动从你电脑获取ip地址和掩码计算需要扫描的子网地址,然后自动扫描,需要扫哪些端口自己改一下就是了。
let net = require("net");
let os = require("os");
function int2ip(int) {
return [ (int >>> 24) & 255, (int >>> 16) & 255, (int >>> 8) & 255, int & 255 ].join(".");
}
function ip2int(ip) {
return ip.split(".").reduce((int, v) => int * 256 + +v);
}
let ip, mask, found;
let ifaces = os.networkInterfaces();
for (let ifname in ifaces) {
let iface = ifaces[ifname];
for (let addr of iface) {
if (!addr.internal && addr.family === "IPv4") {
ip = addr.address;
mask = addr.netmask;
found = true;
break;
}
}
if (found) {
break;
}
}
if (!ip) {
console.log("No usable ipv4 network");
process.exit(-1);
}
console.log("Scanning on", ip, mask);
ip = ip2int(ip);
mask = ip2int(mask);
let net_num = ip & mask;
let max_host_num = ip2int("255.255.255.255") & ~mask;
for (let host_num = 1; host_num < max_host_num; host_num++) {
let ip = int2ip(net_num + host_num);
for (let port of [2121, 8000]) {
let socket = new net.Socket();
socket.setTimeout(1500);
socket.on("connect", function() {
console.log("Found", ip, port);
socket.destroy();
});
socket.on("error", function() {
socket.destroy();
});
socket.on("timeout", function() {
socket.destroy();
});
socket.connect(port, ip);
}
}
比如我一般常用8000(http)和2121(ftp),这是我扫出来的结果:
Scanning on 192.168.196.88 255.255.255.0
Found 192.168.196.88 8000
Found 192.168.196.130 2121
Top comments (0)