Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Forked from c4milo/is-port-available.js
Created October 2, 2013 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengkiardo/6789740 to your computer and use it in GitHub Desktop.
Save hengkiardo/6789740 to your computer and use it in GitHub Desktop.
'use strict';
// pick a random port between 1025 and 65535
// check using netstat to see if port is used
var exec = require('child_process').exec;
var port;
var _port;
var interval;
var handle = function(err, stdout, stderr) {
if (!stdout.trim().length) {
console.log('port ' + _port + ' available!');
port = _port;
clearInterval(interval);
}
};
interval = setInterval(function() {
_port = Math.floor(Math.random() * 65535) + 1025;
exec('netstat -na | grep ' + _port, handle);
}, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment