-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathportScan3.js
More file actions
59 lines (51 loc) · 1.2 KB
/
Copy pathportScan3.js
File metadata and controls
59 lines (51 loc) · 1.2 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
var net = require('net');
// *poke!*...is anybody home?
function poke(port,host) {
var Socket = net.Socket;
var socket = new Socket(),
timeout = 400,
status = null,
error = null,
connectionRefused = null;
// <connect> / connection established
socket.on('connect', function(){
status = 'open';
console.log(port + ' ' +status);
socket.destroy();
});
// <timeout> / no-reponse from host
socket.setTimeout(timeout);
socket.on('timeout', function(){
status = 'closed';
socket.destroy();
console.log(port + ' ' +status);
});
// <error> / connection error handling
socket.on('error', function(exception){
if (exception.code != "ECONNREFUSED")
error = exception.code;
console.log(error);
});
// <close> / connection closing
socket.on('close', function(exception){
if(exception && !connectionRefused){
error = exception;
console.log(error);
}else if(status == 'closed'){
error = null;
}
});
// <connect> / where the magic happens
socket.connect(port, host);
}
// specify port range(min,max)
function scan(min,max,host){
while (min<=max){
poke(min,host);
min++;
}
}
//example usage:
poke(80, 'google.com');
scan(440,443,'google.io');
scan(7998,8000,'127.0.0.1');