gpt4 book ai didi

node.js - 如何在node js中使用node-ssh断开ssh连接

转载 作者:搜寻专家 更新时间:2023-10-31 22:36:45 25 4
gpt4 key购买 nike

任何人都可以帮助我,如何在 node js 中使用 node-ssh 模块断开 ssh 连接。还有如何处理错误。

我的代码是

 driver = require('node-ssh');

ssh = new driver({
host: '192.168.*.*',
username: 'user',
password: 'password',
privateKey : require('fs').readFileSync('/tmp/my_key')
});

ssh.connect().then(function() {
/*
some code
*/

},function(error) {
console.log(error);

});

请帮忙。

最佳答案

使用dispose 方法。示例:

const node_ssh = require('node-ssh');
const ssh = new node_ssh();

ssh.connect({
host: 'XXX',
username: 'YYY',
privateKey: 'ZZZ'
}).then(resp => {
console.log(resp);
ssh.dispose();
});

关于node.js - 如何在node js中使用node-ssh断开ssh连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30392921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com