如何打开持久的 SSH 客户端来制作基于 Web 的文件管理器?

问题描述 投票:0回答:3

我目前正在使用

shelljs
在每次需要使用ssh功能时建立连接。

class Connection {
  constructor(id, path) {
    this.id = id;
    this.path = path;
  }

  exec(silent, ...args) {
    const { exec } = shellJS;
    const execCmd = exec(
      ['ssh ', ...args].join(' '),
      { silent }
    );
    // FormError(execCmd);
    return execCmd;
  }
}

class FileManager {
  constructor(connection) {
    this.connection = connection;
  }

  async ls(path = '') {
    const newPath = path ? `'${path}/'` : '';
    const listing = this.connection.exec(false, `"ls -l ${newPath}"`);
    return parseEntries(listing.stdout);
  }
}

EDIT1:布拉德回答后,安装可能无法在这里工作,因为

  1. 我们对该服务器的访问权限非常有限(不确定是否可以安装)
  2. 这里的代码有些简化。真正的流程是:

    Local -> SSH Server
    -> AnAppThatManagesCloudBuckets <command>
    -> Output
    
javascript node.js shell
3个回答
1
投票

您的代码容易受到命令注入的影响,并且本质上是不安全的,并且对于边缘情况可能不可靠,例如带有保留字符的路径/文件名。

我会推荐一种完全不同的方法。使用 SSHFS 挂载远程文件系统。然后,使用标准的 Node.js fs API 做目录列表等。


0
投票

经过更多研究,发现了 3 个可用于在后台创建持久 SSH 客户端的软件包:

正如@ChrisG 所说。

  1. ssh2
    基本上是用 JS 编写的 SSH 模块
  2. pty.js
    一个伪终端模拟器
  3. node-pty
    pty.js
  4. 的叉子

0
投票

我使用这个基于网络的免费软件双面板 ssh 文件管理器来完成我所有的 OP 任务 https://netbro.org/

© www.soinside.com 2019 - 2024. All rights reserved.