Skip to content
Browse files

updated api

changed structure
updated readme
added .gitignore
  • Loading branch information...
1 parent 6581490 commit 2cdf9b69ba0d31d81526c9531015e4b28c66d76c @danielzzz committed Nov 23, 2011
Showing with 72 additions and 36 deletions.
  1. +16 −0 .gitignore
  2. +10 −0 README
  3. +0 −26 example.js
  4. +29 −0 example/example.js
  5. +3 −0 index.js
  6. +14 −10 { → lib}/portchecker.js
View
16 .gitignore
@@ -0,0 +1,16 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+*.tgz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
View
10 README
@@ -9,6 +9,16 @@ DESCRIPTION
This library allows for easy and simple tcp port checking.
It can be used for determining if a given port is open, or to find a first available port
+API:
+getFirstAvailable(startPort, endPort, host, cb)
+returns first available port in a given range cb(port, host)
+
+getAllOpen(startPort, endPort, host, cb)
+returns array of available ports for a given hosts in a given range cb(openPorts, host)
+
+isOpen(port, host, callback)
+returns true if a given host:port is open cb(isOpen, port, host)
+
EXAMPLE:
View
26 example.js
@@ -1,26 +0,0 @@
-var portchecker = require('./portchecker');
-
-var host = '192.168.1.1',
- startPort = 80,
- endPort = 8080;
-
-
-//------ check first available port between start and endPort on host ---------------
-portchecker.getAllOpen(startPort, endPort, host, function(openPorts, host) {
- openPorts.length && console.log('open ports on ' + host + ': ' + openPorts.join(', '));
- !openPorts.length && console.log('no open ports in given range on ' + host);
-});
-
-//portchecker.getFirstAvailable(startPort, endPort, host, function(p, host) {
-// if (p === -1) {
-// console.log('no free ports found on ' + host + ' between ' + startPort + ' and ' + endPort);
-// } else {
-// console.log('the first free port found on ' + host + ' between ' + startPort + ' and ' + endPort + ' is ' + p);
-// }
-//});
-
-
-//---- check if the port is open ---------------------------------------------------
-//portchecker.isOpen(80, 'localhost', function(isOpen){
-// console.log('port 80 on localhost is ' + (isOpen ? 'open' : 'closed'));
-//})
View
29 example/example.js
@@ -0,0 +1,29 @@
+var portchecker = require('../index');
+
+var host = '192.168.1.1',
+ startPort = 50,
+ endPort = 90;
+
+
+//------ check first available port between start and endPort on host ---------------
+console.log('checking ports');
+portchecker.getAllOpen(startPort, endPort, host, function(openPorts, host) {
+ openPorts.length && console.log('open ports on ' + host + ' between ' + startPort + ' and ' + endPort + ' are: ' + openPorts.join(', '));
+ !openPorts.length && console.log('no open ports in given range on ' + host);
+});
+
+portchecker.getFirstAvailable(startPort, endPort, host, function(p, host) {
+ if (p === -1) {
+ console.log('no free ports found on ' + host + ' between ' + startPort + ' and ' + endPort);
+ } else {
+ console.log('the first free port found on ' + host + ' between ' + startPort + ' and ' + endPort + ' is ' + p);
+ }
+});
+
+
+//---- check if the port is open ---------------------------------------------------
+// wait max 1 sec for result (defautl 400 milisecs)
+portchecker.setTimeout(1000);
+portchecker.isOpen(80, 'google.com', function(isOpen, port, host){
+ console.log('port '+port+' on '+host+' is ' + (isOpen ? 'open' : 'closed'));
+})
View
3 index.js
@@ -0,0 +1,3 @@
+
+
+module.exports = require('./lib/portchecker.js');
View
24 portchecker.js → lib/portchecker.js
@@ -7,9 +7,9 @@
* Use it for checking if a service is up or to find available ports on a machine
*/
-var sys = require('sys');
+var sys = require('util');
var net = require('net');
-
+var timeout = 400; //miliseconds
@@ -18,7 +18,7 @@ exports.getFirstAvailable = function (startPort, endPort, host, callback) {
if (startPort>endPort) {
throw new Error('portchecker: startPort must be lower than endPort');
}
- console.log('looking for an available port in ' + startPort + '-' + endPort + ' on ' + host);
+ //console.log('looking for an available port in ' + startPort + '-' + endPort + ' on ' + host);
var notFree = false;
var currentPort = startPort;
@@ -31,7 +31,7 @@ exports.getFirstAvailable = function (startPort, endPort, host, callback) {
//---- return -1 if we checked all ports from the range already
if (currentPort>endPort) {callback(-1, host); return; };
- console.log('checking :' + currentPort);
+ //console.log('checking :' + currentPort);
exports.isOpen(currentPort, host, onCheckPort);
currentPort++;
}
@@ -46,7 +46,7 @@ exports.getAllOpen = function (startPort, endPort, host, callback) {
if (startPort>endPort) {
throw new Error('portchecker: startPort must be lower than endPort');
}
- console.log('looking for open ports between ' + startPort + '-' + endPort + ' on ' + host);
+ //console.log('looking for open ports between ' + startPort + '-' + endPort + ' on ' + host);
var notFree = false,
openPorts = [];
var currentPort = startPort;
@@ -62,7 +62,7 @@ exports.getAllOpen = function (startPort, endPort, host, callback) {
//---- return -1 if we checked all ports from the range already
if (currentPort>endPort) {callback(openPorts, host); return; };
- console.log('checking :' + currentPort);
+ //console.log('checking :' + currentPort);
exports.isOpen(currentPort, host, onCheckPort);
currentPort++;
}
@@ -73,15 +73,15 @@ exports.getAllOpen = function (startPort, endPort, host, callback) {
}
-exports.isOpen = function (p, host, callback) {
+exports.isOpen = function (port, host, callback) {
var isOpen = false;
- var conn = net.createConnection(p, host);
- var timeout = 400; //miliseconds
+ var conn = net.createConnection(port, host);
+
var timeoutId = setTimeout(function() {onClose();}, timeout);
var onClose = function() {
clearTimeout(timeoutId);
delete conn;
- callback(isOpen);
+ callback(isOpen, port, host);
};
var onOpen = function() {
@@ -95,4 +95,8 @@ exports.isOpen = function (p, host, callback) {
conn.on('connect', onOpen);
}
+exports.setTimeout = function(t) {
+ timeout = t;
+}
+

0 comments on commit 2cdf9b6

Please sign in to comment.
Something went wrong with that request. Please try again.