我正在尝试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。有人可以帮我解决这个问题吗?
我在另一个异步函数中调用异步函数时犯了一个错误,如下所示,
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] 删除。
我来说两句