Created
August 6, 2013 05:59
-
-
Save ssddi456/6162378 to your computer and use it in GitHub Desktop.
check used linux port in range [x,y)
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 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