-1
我想ping每個IP地址的數組/ JSON的可達性。這裏有一個簡單的場景:在for循環中使用Node.js child_process.exec() ping一個IP地址
var onlineCams = {};
for (var i = 0; i < ipList.cameras.length; i++) {
var currentIp = ipList.cameras[i].ip;
var currentIpStatus = "";
var shellCmd = 'ping -c 1 ' + currentIp + ' | grep icmp* | grep bytes | wc -l';
var child = exec(shellCmd, function (error, stdout, stderr) {
if (stdout.toString().trim() == '1')
currentIpStatus = "Online";
else
currentIpStatus = "Offline";
onlineCams[currentIp] = currentIpStatus;
});
}
輸出示例:
// console.log(ipList.cameras);
[ { id: 0, ip: '192.168.79.139' },
{ id: 1, ip: '192.168.79.16' } ]
// console.log(onlineCams);
{ '192.168.79.16': 'Online' }
有在onlineCams
只有一個IP地址時,應該有兩個。出於某種原因,.139被跳過了,我猜測這是因爲在返回.139的結果之前exec
線程會覆蓋它自己。有沒有解決這個問題的方法?