Skip to content

Instantly share code, notes, and snippets.

@ssddi456
Created August 6, 2013 05:59
Show Gist options
  • Save ssddi456/6162378 to your computer and use it in GitHub Desktop.
Save ssddi456/6162378 to your computer and use it in GitHub Desktop.
check used linux port in range [x,y)
var child_process = require('child_process');
module.exports = function check_linux_port ( port_range, done ) {
var check = child_process.exec('netstat -tln');
var res = '';
check.stdout.on('data',function(chuck) {
res+= chuck;
});
check.on('exit',function(code) {
if( code == 0 ){
res = res.split('\n').slice('1');
res = res.map(function( line ) {
var host_port = line.match(/(\d+\.\d+\.\d+\.\d+)\:(\d+)/);
if( host_port ){
return host_port[2];
}
}).filter(function( port ) {
if( port ){
port = +port;
return port >= port_range[0] && port < port_range[1];
}
}).sort();
done( null, res );
} else {
done(code);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment