简体   繁体   English

AWS JavaScript SDK:从 ECS 执行命令中检索 Shell Output

[英]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.

相关问题 如何执行 AWS SSM 发送命令以从 Lambda 运行带有 arguments 的 shell 脚本? - How to execute AWS SSM send command to run shell script with arguments from Lambda? 在 AWS SSM 中检索命令调用 - Retrieving command invocation in AWS SSM 从 AWS ECS 运行任务中获取数据返回到 AWS Lambda - Get data from AWS ECS run task back to AWS Lambda 适用于 PHP 的 S3 S3EncryptionClient AWS 开发工具包:从实例配置文件元数据服务器检索凭证时出错 - S3 S3EncryptionClient AWS SDK for PHP: Error retrieving credentials from the instance profile metadata server AWS Lambda 执行 shell npm 安装报错 - AWS Lambda execute shell npm install error 如何使用 AWS Javascript SDK 从 EC2 实例连接其他 AWS 云服务? - How to connect other AWS cloud service from an EC2 instance using AWS Javascript SDK? 允许在 AWS ECS 容器上执行命令的 IAM 角色 - IAM role to allow command execution on AWS ECS containers 在 ECS 集群中运行来自 AWS ECR 的公共镜像 - Running a public image from AWS ECR in ECS Cluster 是否可以将 docker 图像从公共 Docker 中心移动到 aws ecs? - Is it possible to Move docker images from Public Docker hub to aws ecs? 如何从 shell 脚本中确认 aws cloudformation validate-template 命令? - How to confirm the aws cloudformation validate-template command from shell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM