cla*_*rez 10 networking node.js
我想知道是否有办法扫描本地网络的IP范围以查找特定号码的开放端口.
基本上我正在寻找在nodejs不知道其IP地址的情况下找到特定类型的客户端.在这种情况下,RFID读取器侦听端口14150.
我希望这次扫描很快,所以我不希望每个IP地址之间有很长的超时.它们都应该相当快速地发生,可能在几秒钟内最多可达到最多255个客户端的整个本地IP范围,不包括我自己的IP.
我写的代码,我想要做什么,但它是非常缓慢.我想知道怎样才能使这个速度通过连接炽烈和失控如果连接不能对一个给定的IP 20ms内作出.我想捕获一个数组中的实际连接,然后我可以将其用于其他目的.
var net = require('net'); // Required to create socket connections
var ip = 254; //IP address to start with on a C class network
function checkConnect () {
ip--;
var thisIP = '192.168.1.' + ip; //concatenate to a real IP address
var S = new net.Socket();
S.connect(80, thisIP);
if(ip > 0) { checkConnect(); }
S.on('connect', function () { console.log('port 80 found on ' + thisIP); });
S.on('error', function () { console.log('no such port on ' + thisIP); });
S.end();
}
checkConnect();
Run Code Online (Sandbox Code Playgroud)
小智 19
我已经为你制作了https://github.com/eviltik/evilscan.(今天刚刚发布v0.0.3)
安装:
npm install -g evilscan
Run Code Online (Sandbox Code Playgroud)
用法(端口列表+端口范围):
root@debian:~# evilscan --target=192.168.0.0/24 --port=21-446,5900 --concurrency=100 --progress
192.168.0.3:5900|open
192.168.0.26:53|open
192.168.0.26:111|open
192.168.0.26:81|open
192.168.0.26:23|open
Scanned 192.168.0.253:446 (100%)
Run Code Online (Sandbox Code Playgroud)
小贴士:
对于非常快速的扫描,您可以使用"并发"参数,超过1000,但您必须首先更新您的linux的ulimit参数:
ulimit -u unlimited
Run Code Online (Sandbox Code Playgroud)
希望这有帮助.
以前的答案都没有真正起到我需要的作用.我找到了一个更轻的重量替代品.有了这个解决方案,我很快得到了我的解 我的下一次升级将是根据当前子网指定一系列主机.我想我会想把它限制在前254个客户端,所以它不会太过分.这是代码:
//LLRP DEVICE SCANNER
var net = require('net'), Socket = net.Socket;
var checkPort = function(port, host, callback) {
var socket = new Socket(), status = null;
// Socket connection established, port is open
socket.on('connect', function() {status = 'open';socket.end();});
socket.setTimeout(1500);// If no response, assume port is not listening
socket.on('timeout', function() {status = 'closed';socket.destroy();});
socket.on('error', function(exception) {status = 'closed';});
socket.on('close', function(exception) {callback(null, status,host,port);});
socket.connect(port, host);
}
var LAN = '192.168.1'; //Local area network to scan (this is rough)
var LLRP = 5084; //globally recognized LLRP port for RFID readers
//scan over a range of IP addresses and execute a function each time the LLRP port is shown to be open.
for(var i=1; i <=255; i++){
checkPort(LLRP, LAN+'.'+i, function(error, status, host, port){
if(status == "open"){
console.log("Reader found: ", host, port, status);
}
});
}
Run Code Online (Sandbox Code Playgroud)
您可以使用arp命令获取首先处于活动状态的设备列表.在盒子外思考;)您不必盲目扫描所有设备.
var child = require("child_process");
var async = require("async");
var net = require("net");
var os = require("os");
function scan(port, cb){
var hosts = {};
var result = [];
async.series([
function scan(next, c){
if(c == 1){
next(); return;
}
// scan twice because arp sometimes does not list all hosts on first time
child.exec("arp -n | awk '{print $1}' | tail -n+2", function(err, res){
if(err) cb(err);
else {
var list = res.split("\n").filter(function(x){return x !== "";});
list.map(function(x){
hosts[x] = x;
});
}
scan(next, 1);
});
},
function(next){
// if you want to scan local addresses as well
var ifs = os.networkInterfaces();
Object.keys(ifs).map(function(x){
hosts[((ifs[x][0])||{}).address] = true;
});
// do the scan
async.each(Object.keys(hosts), function(x, next){
var s = new net.Socket();
s.setTimeout(1500, function(){s.destroy(); next();});
s.on("error", function(){
s.destroy();
next();
});
s.connect(port, x, function(){
result.push(x);
s.destroy();
next();
});
}, function(){
next();
});
}
], function(){
cb(null, result);
});
}
scan(80, function(err, hosts){
if(err){
console.error(err);
} else {
console.log("Found hosts: "+hosts);
}
});
Run Code Online (Sandbox Code Playgroud)
您还可以使用arp-scan实用程序,它更可靠.但是arp-scan需要root访问才能工作,所以最好只使用arp.它几乎可以在每个Linux机器上使用.
| 归档时间: |
|
| 查看次数: |
13135 次 |
| 最近记录: |