2016-04-05 12 views
2

私はssh-exec npmを使用しています。リモートのsshコマンドの結果を取得し、解析して配列に格納したいと考えています。代わりにstdoutにパイプされているので、変数に取り込む方法がわかりません。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 

答えて

1

あなたはパイプを使用したくない場合は、これをやってみています。まだ非同期であるため、関数からこのデータを返すことを検討している場合は、コールバックや約束などを使用する必要があります。

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 
}); 

streamsインターフェイス、特に読み取り可能なインターフェイスを調べる必要があります。

関連する問題