Skip to content

Instantly share code, notes, and snippets.

@c4milo
Created October 1, 2013 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save c4milo/6786309 to your computer and use it in GitHub Desktop.
Save c4milo/6786309 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