我想使用 Node.js 或 JavaScript 建立 SSH 连接,以便通过网页连接它并运行命令。我怎样才能做到这一点?
最佳答案
有一个非常好的库,node-ssh 。它也支持 promise 。看看那个。
从官方文档复制的代码:
var path, node_ssh, ssh, fs
fs = require('fs')
path = require('path')
node_ssh = require('node-ssh')
ssh = new node_ssh()
ssh.connect({
host: 'localhost',
username: 'steel',
privateKey: '/home/steel/.ssh/id_rsa'
})
或者
ssh.connect({
host: 'localhost',
username: 'steel',
privateKey: fs.readFileSync('/home/steel/.ssh/id_rsa')
})
.then(function() {})
Edit-I have tried this code in my system and its working
var path, node_ssh, ssh,
fs
fs = require('fs')
path = require('path')
node_ssh = require('node-ssh')
ssh = new node_ssh()
ssh.connect(
{ host: 'Your host', username: 'username', password: 'yourpass',
})
.then(function(response) {console.log(response)})
关于node.js - 如何使用 Node.js 建立 SSH 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56110005/