Last active
January 26, 2016 02:22
-
-
Save jcarroyo/df64a1bcd0ae998bb7f2 to your computer and use it in GitHub Desktop.
A simple port scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var parameters = process.argv.slice(2, process.argv.length); | |
var timeOut = 2000; | |
console.log(parameters) | |
if(parameters.length == 1 && parameters[0] == "help"){ | |
printHelp(); | |
} | |
if(parameters.length != 2){ | |
printHelp(); | |
} | |
main(parameters[0], parameters[1]); | |
function printHelp(){ | |
console.log("node scanner.js ip portRange"); | |
console.log("sample: node portScanner.js.js 192.168.1.33 80-2000"); | |
process.exit(); | |
} | |
function main(ip, portRange){ | |
var ports = portRange.split('-'); | |
if(ports.length != 2){ | |
printHelp(); | |
} | |
var pi = ports[0], pe = ports[1]; | |
while(pi <= pe){ | |
(function(ip ,port){ | |
var s = new net.Socket(); | |
//https://nodejs.org/dist/latest-v4.x/docs/api/net.html#net_socket_connect_options_connectlistener | |
s.connect({ | |
port: port, | |
host: ip | |
}, function(){ | |
console.log("host", ip, "port", port, "alive") | |
clearTimeout(to); | |
s.destroy(); | |
}) | |
s.on('data', function(){ | |
s.destroy(); | |
}) | |
s.on('error', function(){ | |
s.destroy(); | |
}) | |
var to = setTimeout(function(){ | |
console.log("host"); | |
console.log("host", ip, "port", port, "dead"); | |
s.destroy(); | |
}, timeOut) | |
})(ip, pi); | |
pi++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment