-1

在上一个问题中,我询问是否可以使用 node.js 从远程 SSH 会话运行 telnet 会话。我通常通过 SSH putty 会话连接到 linux 服务器,然后 telnet 到另一个 idirect / 调制解调器。我想使用 node.js 以编程方式执行此操作。根据收到的答复,我知道我必须运行一个期望脚本来执行所需的 telnet 命令。

是否可以使用ssh2shellor运行脚本simple-ssh

4

2 回答 2

0

我使用了 node-ssh。

您可以连接到实例,并且可以像这样使用它:

let sshConnection = ssh.connect({
    host: <host>,
    username: <username>,
    privateKey: '/path/to/id-rsa'
  })

  sshConnection.then(() => {
    ssh.putFile('./script.py', '/tmp/script.py').then(() => {
      log('The File thing is done')
    }, (error) => {
      log('Somethings wrong', error)
    })
  })

  sshConnection.then(() => {
    ssh.execCommand(`echo hello-world`, { cwd: '/home' })
    .then((result) => {
      log(`result.stdout = ${result.stdout}`)
      log(`result.stderr = ${result.stderr}`)
    })
  })

于 2019-02-08T22:25:59.407 回答
0

这是完全可行的,根据我对您问题的理解,您想通过 SSH 连接到服务器,然后运行脚本?请详细说明一下。

有一些工具可以通过 Node.js 运行 shell 命令: - ShellJS - Exec-sh

需要注意的一件事是,您必须将脚本移植到它们的语法中的那些工具,但它们仍然是 shell 命令,它们也提供错误处理。

于 2019-02-08T17:27:11.733 回答