Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从stdout读取_Javascript_Node.js - Fatal编程技术网

Javascript 从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:/

我正在使用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:/) 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
});
您想看看接口,特别是可读的接口