我正在Windows VM上运行量角器,并且需要在Linux VM上执行一些命令。我正在尝试使用SSH执行相同的操作。我试过使用'simple-ssh','remote-exec'和'ssh-exec。它们所有的问题都是相同的,量角器测试已完成而没有任何错误,但未建立SSH连接。奇怪的是,它也不会引发任何错误,我尝试提供错误的IP,但是仍然不会引发任何错误。我已经在同一台计算机上尝试过python上的SSH,它可以正常工作。
这里是我直接尝试使用的文档中的一段代码。
var ssh = new SSH({
host: 'xx.xx.xxx.xx',
user: 'xxxxx',
pass: 'xxxxx'
});
ssh.exec('ls -lh', {
out: function(stdout) {
console.log(stdout);
}
}).start();
想通了。
我使用ssh2程序包建立了一个交互式SSH会话。然后我在茉莉花2中使用done()将其与茉莉花同步。
使用Maciej Ciach的solution解决同步问题。
这里是可以完美运行的'It'块
it("trying ssh connection", function (done) {
var conn = new Client();
conn.on('ready', function () {
console.log('Client :: ready');
conn.shell(function (err, stream) {
if (err) throw err;
stream.on('close', function () {
console.log('Stream :: close');
conn.end();
}).on('data', function (data) {
console.log('OUTPUT: ' + data);
});
stream.end('ls \nexit\n');
done();
});
}).connect({
host: 'xx.xx.xxx.xx',
port: 22,
username: 'x',
privateKey: require('fs').readFileSync('file_path')
});
})
显然,您首先需要将公共ssh密钥添加到服务器上的受信任密钥列表中。您可以阅读有关它的内容here。
如果您在Windows上,请在Powershell中执行这些命令。