[英]AWS JavaScript SDK: Retrieving Shell Output from ECS Execute Command
I'm running ExecuteCommandCommand successfully using the AWS JavaScript SDK v3, but I'm unable to find out how to log the shell output. The ExecuteCommandCommandOutput
interface does not include anything that would point to that, and by logging it after a successful execution I indeed to not see the results.我正在使用 AWS JavaScript SDK v3 成功运行ExecuteCommandCommand ,但我无法找到如何记录 shell
ExecuteCommandCommandOutput
接口不包含任何指向它的内容,通过在成功执行后记录它,我确实到不了结果。
I was able to accomplish this in Node using npm packages 'ssm-session' and 'ws'.我能够使用 npm 包“ssm-session”和“ws”在 Node 中完成此操作。
First, executing the command using ECS ExecuteCommandCommand:首先,使用 ECS ExecuteCommandCommand 执行命令:
const { ECSClient, ListTasksCommand, ExecuteCommandCommand } = require("@aws-sdk/client-ecs");
const ecs = new ECSClient({ region })
const executeCommand = new ExecuteCommandCommand( {cluster, interactive: true, command, task})
const response = await ecs.send(executeCommand)
const { streamUrl, tokenValue } = response.session
Then, you can use the following snippet to log the output using the streamUrl and tokenValue connected above.然后,您可以使用以下代码段使用上面连接的 streamUrl 和 tokenValue 记录 output。
const WebSocket = require("ws");
const { ssm } = require("ssm-session");
const util = require("util");
const textDecoder = new util.TextDecoder();
const textEncoder = new util.TextEncoder();
const termOptions = {
rows: 34,
cols: 197,
};
const connection = new WebSocket(streamUrl);
process.stdin.on("keypress", (str, key) => {
if (connection.readyState === connection.OPEN) {
ssm.sendText(connection, textEncoder.encode(str));
}
});
connection.onopen = () => {
ssm.init(connection, {
token: tokenValue,
termOptions: termOptions,
});
};
connection.onerror = (error) => {
console.log(`WebSocket error: ${error}`);
};
connection.onmessage = (event) => {
var agentMessage = ssm.decode(event.data);
ssm.sendACK(connection, agentMessage);
if (agentMessage.payloadType === 1) {
process.stdout.write(textDecoder.decode(agentMessage.payload));
} else if (agentMessage.payloadType === 17) {
ssm.sendInitMessage(connection, termOptions);
}
};
connection.onclose = () => {
console.log("websocket closed");
};
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.