我有pids /端口列表(通过在NodeJS中使用cmd行)。
我只想过滤那些仅运行节点服务器的pid /端口。(不使用pm2)
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 844
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:623 0.0.0.0:0 LISTENING 8488
TCP 0.0.0.0:2701 0.0.0.0:0 LISTENING 4432
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 19908
TCP 0.0.0.0:3100 0.0.0.0:0 LISTENING 23688
TCP 0.0.0.0:3200 0.0.0.0:0 LISTENING 12252
TCP 0.0.0.0:3300 0.0.0.0:0 LISTENING 18616
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 1152
TCP 0.0.0.0:5700 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:7680 0.0.0.0:0 LISTENING 10848
TCP 0.0.0.0:9000 0.0.0.0:0 LISTENING 16800
TCP 0.0.0.0:9012 0.0.0.0:0 LISTENING 7416
TCP 0.0.0.0:16992 0.0.0.0:0 LISTENING 8488
TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING 648
TCP 0.0.0.0:49665 0.0.0.0:0 LISTENING 1572
TCP 0.0.0.0:49666 0.0.0.0:0 LISTENING 1944
TCP 0.0.0.0:49667 0.0.0.0:0 LISTENING 2416
TCP 0.0.0.0:49668 0.0.0.0:0 LISTENING 3208
TCP 0.0.0.0:49669 0.0.0.0:0 LISTENING 808
TCP 0.0.0.0:49670 0.0.0.0:0 LISTENING 808
TCP 0.0.0.0:49671 0.0.0.0:0 LISTENING 720
TCP 0.0.0.0:49679 0.0.0.0:0 LISTENING 4964
TCP 10.191.74.99:139 0.0.0.0:0 LISTENING 4
TCP 10.191.74.99:5040 0.0.0.0:0 LISTENING 440
TCP 10.191.74.99:7680 10.191.63.231:57996 SYN_RECEIVED 10848
.......
答案 0 :(得分:0)
请查看代码,并为我提供更好的方法来获得预期的结果。
(function(port) {
// console.log('CHECK: ' + port);
var s = new net.Socket();
s.setTimeout(timeout, function() { s.destroy(); });
s.connect(port, host, function() {
find('port', port)
.then(function (list) {
if(list[0].name.split(".")[0] === "node") {
list[0].port = port;
nodePid.push(list);
console.log(nodePid);
}
}, function (err) {
console.log(err.stack || err);
})
// console.log('OPEN: ' + port);
// we don't destroy the socket cos we want to listen to data event
// the socket will self-destruct in 2 secs cos of the timeout we set, so no
//worries
});
// if any data is written to the client on connection, show it
s.on('data', function(data) {
console.log(port +': '+ data);
s.destroy();
});
s.on('error', function(e) {
// silently catch all errors - assume the port is closed
s.destroy();
});
})(port);
start++;
}`