var child = require('child_process');
var array = [
[{ "ip": "172.18.59.180", "name": "node1" }],
[{ "ip": "172.18.80.80", "name": "node2" }]
];
array.forEach(element => {
console.log(element[0].name);
p(element);
});
function p(element) {
new Promise((res, rej) => {
console.log(element[0].name);
res(element);
}).then((element) => {
console.log('ping -c 10 -w 5 ' + element[0].ip);
var connecting = false;
var fab_cp = child.exec('ping -c 10 -w 5 ' + element[0].ip);
fab_cp.stdout.on("data", function(data) {
data = data.toString(data);
if (data.indexOf('0% packet loss') > -1) {
console.log('connecting ok');
connecting = true;
} else if (data.indexOf('100% packet loss') > -1) {
console.log('connecting err ~');
console.log(data);
} else if (data.indexOf('Destination Host Unreachable') > -1) {
console.log('Destination Host Unreachable ~');
console.log(data);
} else {
console.log('connecting Intermittent ~');
console.log(data);
}
});
fab_cp.on("exit", function(code, signal) {
console.log(code, ',', signal);
console.log('connecting status :', connecting);
});
});
}