在远程服务器上的NodeJS中使用ssh2-promise执行Linux命令

cslrnr:

我正在尝试yum install <package_name>使用ssh2-promise软件包在远程linux服务器上执行命令,但是我无法获取命令响应以进行进一步的处理和验证。

我尝试了以下方法,

// Node:33528) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
(async function(){
        try {
            const data = await this.ssh.exec("yum  repolist all");
            console.log("resp: ", data); 
            } catch(e) {
                console.log(e)
            }
        })();  // This fails 

        const socket = await this.ssh.spawn("yum repolist all");
        socket.on('data', function(data) {
              console.log("resp: " , data); // I get binary data not the human readable output
        });


        // Node:33528) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
        this.ssh.exec("yum install <name>").then((data) => {
            console.log("resp: yum repolist all output: ", data); // This also fails and throws exception

        });

        // Throws again (Node:33528) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
        const output = await this.ssh.exec("yum upgrade <name>");
        console.log("resp: ", output) 

我也尝试使用适当的try catch块,但仍然抛出unhandledPromise Exception。有人可以帮我解决这个问题吗?

cslrnr:

我在另一个异步函数中调用异步函数时犯了一个错误,如下所示,

public async runCommand() {
     (async function () {
    try {
        const data = await ssh.exec("yum repolist all");
        console.log("resp: ", data);
    } catch (e) {
        console.log("Error - " + e);
    }
    })(); 
}

像下面那样更改了功能,它起作用了。

public async runCommand() { 
    try {
        const data = await ssh.exec("yum repolist all");
        console.log("resp: ", data);
    } catch (e) {
        console.log("Error - " + e);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在远程服务器上的NodeJS中使用ssh2-promise执行Linux命令

nodejs在远程linux服务器上执行命令

使用SSH在远程Linux服务器上运行命令

通过SSH在远程服务器上执行命令

使用脚本在ssh服务器上远程运行命令

Rundeck:使用 ssh 在远程服务器上运行命令

使用SSH执行内联命令远程服务器

如何使用 PuTTY 从远程 SSH 服务器上的 PHP 中执行的命令获取结果?

去执行ssh命令,并且不能在远程服务器上终止该命令

ssh:在远程服务器上执行命令时出现终端问题

如何使用“双” ssh 在远程 Linux 服务器上使用 Visual Studio 进行调试?

如何使用nohup通过ssh在远程服务器上执行本地bash脚本

使用 ssh 的远程服务器上的 Chmod 命令不起作用

在ssh和特殊字符的远程服务器上使用sed命令的bash脚本

golang:在远程服务器上执行shell命令

Capistrano 3在远程服务器上执行任意命令

Ansible不在远程服务器上执行命令

groovy 在远程服务器上执行 shell 命令

Bash for循环以在远程服务器上执行命令

命令无法在远程服务器上正确执行

通过2个脚本在远程服务器上执行命令

如何在远程Linux服务器中使用Maven?

在远程服务器上使用shell变量执行awk命令

如何使用Ansible在远程服务器上执行动态“ top”命令

使用批处理文件和PuTTY在远程服务器上执行sftp命令

linux远程执行命令

无法通过远程ssh命令在远程服务器上构建go程序

从远程服务器上的本地服务器执行IMPDP命令时出现问题

从Windows命令行在Linux虚拟机(或服务器)上执行命令