简体   繁体   English

是否可以使用 node.js 使用 ssh2shell 或 simple-ssh 运行脚本?

[英]Is it possible to run a script with ssh2shell or simple-ssh using node.js?

In a previous question, I asked if it was possible to run a telnet session from a remote SSH session using node.js.在上一个问题中,我询问是否可以使用 node.js 从远程 SSH 会话运行 telnet 会话。 I usually connect to a linux server via an SSH putty session, and then telnet to another idirect / modem.我通常通过 SSH putty 会话连接到 linux 服务器,然后 telnet 到另一个 idirect/调制解调器。 I would like to do this programmatically using node.js.我想使用 node.js 以编程方式执行此操作。 From the answer received, I understand that I must run an expect script to execute the required telnet commands.从收到的答案中,我明白我必须运行一个期望脚本来执行所需的 telnet 命令。

Is it possible to run a script using ssh2shell or simple-ssh ?是否可以使用ssh2shellsimple-ssh运行脚本?

It is totally doable, from what I understand in your question is that you want to connect via SSH to a server, and then run a script?这是完全可行的,根据我在您的问题中的理解,您想通过 SSH 连接到服务器,然后运行脚本? Please elaborate a little bit.请详细说明一下。

There are some tools to run shell commands via Node.js : - ShellJS - Exec-sh有一些工具可以通过 Node.js 运行 shell 命令: - ShellJS - Exec-sh

One thing to note is that those tools you have to port your script to their syntax, but they are still shell commands, they offer error handling too.需要注意的一件事是,您必须将脚本移植到它们的语法中的那些工具,但它们仍然是 shell 命令,它们也提供错误处理。

I used node-ssh.我使用了节点 ssh。

You can connect to the instance, and you can use it like this:您可以连接到实例,并且可以像这样使用它:

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}`)
    })
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM