Javascript 从stdout读取
我正在使用ssh-exec-npm,希望获得远程ssh命令的结果,对其进行解析,并将其存储到一个数组中。它被管道传输到stdout,我不知道如何将它放入变量中Javascript 从stdout读取,javascript,node.js,Javascript,Node.js,我正在使用ssh-exec-npm,希望获得远程ssh命令的结果,对其进行解析,并将其存储到一个数组中。它被管道传输到stdout,我不知道如何将它放入变量中 function GetRemoteLinuxIp(hostAddress) { console.log(hostAddress); exec("ifconfig -a | tr -s ' ' | awk -F'[: ]' ' {if (/^[a-z]/) printf $1 \" \"} {if (/inet addr:/
function GetRemoteLinuxIp(hostAddress) {
console.log(hostAddress);
exec("ifconfig -a | tr -s ' ' | awk -F'[: ]' ' {if (/^[a-z]/) printf $1 \" \"} {if (/inet addr:/) print $4 }' ", {
user: 'user',
host: hostAddress,
password: 'password'
}).pipe(process.stdout)
};
输出是
enp0s3 192.168.1.8
enp0s3 192.168.1.9
如果您不想使用管道,请尝试这样做。它仍然是异步的,所以如果您希望从函数返回此数据,您至少必须使用回调或承诺之类的东西
exec("ifconfig -a | tr -s ' ' | awk -F'[: ]' ' {if (/^[a-z]/) printf $1 \" \"} {if (/inet addr:/) print $4 }' ", {
user: 'user',
host: hostAddress,
password: 'password'
}).on('data', function(data) {
console.log('data:', data);
// parse and push items onto an array
}).on('end', function() {
// no more data will be coming in
// resolve your promise with the array,
// or pass it in to your callback from here
});
您想看看接口,特别是可读的接口