2014-07-10 88 views
0

我想從一個碼頭工人容器內運行,並得到有關搬運工集裝箱信息,要做到這一點,我使用Nexpect,但我發現了一個奇怪的錯誤:爲什麼在Nexpect上的SSH命令不起作用?

exports.getIP = function(username, callback){ 
    var nexpect = require('nexpect'); 
    nexpect.spawn("ssh [email protected] \"docker inspect --format '{{ .NetworkSettings.IPAddress }}' " + username + "\"") 
     .expect(" ") 
     .run(function (err, stdout, exitcode) { 
      if (err) { 
       callback(err); 
      } 
      console.log("inside = " + stdout); 
      ip = stdout; 

      callback(null, ip); 
    }); 
}; 

這種方法的錯誤響應是「錯誤:找不到命令:SSH」

但是如果我手動shell中運行:

ssh [email protected] "docker inspect --format '{{ .NetworkSettings.IPAddress }}' test" 

完美的作品。

我也有非常相似的功能來運行使用Nexpect和SSH的Docker,它工作正常。經過一些測試,我認爲它是\字符的東西,但對於這個特定的命令,我必須在字符串上使用引號。

+0

你試過從它的目錄下運行呢?例如'/ usr/bin/ssh'。 –

+0

我剛剛試過並得到:「錯誤:未找到命令:/ usr/bin/ssh」,我在命令shell中嘗試了它並開始工作。 – esdrasportillo

回答

0

您可以使用ssh2 module而不是產卵一個子進程,並使用nexpect的:

var Connection = require('ssh2'); 

var ssh = new Connection(); 
ssh.on('ready', function() { 
    ssh.exec("docker inspect --format '{{ .NetworkSettings.IPAddress }}' test", function(err, stream) { 
    if (err) 
     throw err; 

    stream.on('exit', function(code, signal) { 
     console.log('>> Docker exited with code %d, signal %s', code, signal); 
    }).on('close', function() { 
     // this indicates that there is no more stdout/stderr output, 
     // just like with node's spawn() 
     ssh.end(); 
    }).pipe(process.stdout); 

    stream.stderr.pipe(process.stderr); 
    }); 
}).connect({ 
    host: '172.17.8.101', 
    username: 'core', 
    agent: process.env.SSH_AUTH_SOCK, 
    // or if you have a specific key you want to use: 
    //privateKey: require('fs').readFileSync(process.env.HOME + '/.ssh/id_rsa') 
}); 
+0

我收到此錯誤:錯誤:由主機斷開(PROTOCOL_ERROR):數據包完整性錯誤。經過一些研究,我沒有修復它,我使用的是privateKey方式。 – esdrasportillo

+0

使用代理方式我得到了錯誤:錯誤:在等待握手時超時 – esdrasportillo

相關問題