使用AWS Golang SDK v2的非交互式EC2的Shell。

huangapple go评论91阅读模式
英文:

Non interactive shell of an EC2 using aws golang sdk v2

问题

使用AWS Go SDK v2可以实现相同的功能。您可以使用AWS Systems Manager来执行命令,而无需使用SSH。您可以直接在Go代码中获取输出,并将其打印到控制台上,而无需使用交互式shell。

以下是一个示例代码片段,演示如何使用AWS Go SDK v2执行命令并获取输出:

package main

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/ssm"
	"os"
)

func main() {
	// 创建AWS配置
	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		fmt.Println("无法加载AWS配置", err)
		os.Exit(1)
	}

	// 创建SSM客户端
	client := ssm.NewFromConfig(cfg)

	// 创建命令输入参数
	input := &ssm.SendCommandInput{
		DocumentName:   aws.String("AWS-RunShellScript"),
		InstanceIds:    []string{"your-instance-id"},
		Parameters: map[string][]string{
			"commands": {"ls"},
		},
	}

	// 发送命令并获取输出
	output, err := client.SendCommand(context.TODO(), input)
	if err != nil {
		fmt.Println("无法发送命令", err)
		os.Exit(1)
	}

	// 获取命令输出
	commandOutput, err := client.GetCommandInvocation(context.TODO(), &ssm.GetCommandInvocationInput{
		CommandId:  output.Command.CommandId,
		InstanceId: aws.String("your-instance-id"),
	})
	if err != nil {
		fmt.Println("无法获取命令输出", err)
		os.Exit(1)
	}

	// 打印输出结果
	fmt.Println(commandOutput.StandardOutputContent)
}

请注意,您需要将your-instance-id替换为您实际的EC2实例ID。此示例使用AWS-RunShellScript文档来执行命令,您可以根据需要更改文档名称和命令参数。

希望这可以帮助到您!

英文:

With bash I type this command:

ssh -i key.pem ubuntu@ec2-instance ls

And the result will be something like:

file1
file2
file3

Question:

Can I do the same thing with AWS go SDK v2?

What I need:

  • Use AWS systems manager
  • Do not use ssh.
  • Get the output directly in my go code to print it to the console
  • Don't need an interactive shell

答案1

得分: 1

API可以实现这个功能。不过我对Go SDK不太熟悉。

这里有一个JavaScript SDK的链接(我假设功能是相同的):https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#sendCommand-property

也可以通过命令行完成:

aws ssm send-command --instance-ids "i-INSTANCEID" --document-name "AWS-RunShellScript" --comment "List" --parameters commands=ls --region "ap-northeast-1"

你可以使用list-command-invocations命令获取结果:

aws ssm list-command-invocations --command-id "4f65c2da-NNNN-JJJJ-LLLL-6efc67e6cd5d" --details --region "ap-northeast-1"

使用的用户/角色需要具有ssm:SendCommand和ssm:ListCommandInvocations的访问权限,例如:

   {
        "Effect": "Allow",
        "Action": [
            "ssm:SendCommand",
            "ssm:ListCommandInvocations"
        ],
        "Resource": "*"
    }
英文:

The API allows this. I'm not familar with the go SDK though.

here's a link to the JavaScript SDK (I assume functionality is the same) : https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#sendCommand-property

It can be done by the command line as well:

aws ssm send-command --instance-ids "i-INSTANCEID" --document-name "AWS-RunShellScript" --comment "List" --parameters commands=ls --region "ap-northeast-1"

you get the result with list-command-invocations

aws ssm list-command-invocations --command-id "4f65c2da-NNNN-JJJJ-LLLL-6efc67e6cd5d" --details --region "ap-northeast-1"

The user/role used needs access to ssm:SendCommand and ssm:ListCommandInvocations - e.g.

   {
        "Effect": "Allow",
        "Action": [
            "ssm:SendCommand",
            "ssm:ListCommandInvocations"
        ],
        "Resource": "*"
    }

huangapple
  • 本文由 发表于 2022年4月16日 08:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71890201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定