node.js - 如何使用 Node.js 建立 SSH 连接

标签 node.js ssh

我想使用 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/

相关文章:

node.js - 带有 CouchDB 附件的服务文件?

bash - 如何检查与bash中服务器列表的连接?

git - SSH 克隆/推送到 Bitbucket 禁止

javascript - 使用 Electron 和 React 从目录读取文件名并输出到 JSON 的方法

java - Node.js 和 WebSphere Portal 之间的单点登录

node.js - MongoDB - 使用 nodejs 驱动程序运行并行查询并在最后一个查询完成时关闭数据库连接

javascript - 正确链接 Promise

ubuntu - SSH 使用腻子进入 ubuntu 服务器上的 LXC 容器

linux - 如何从文件重定向密码请求?

java - Intellij Idea 运行/调试配置和 Gradle 任务中的 SSH 隧道