Amazon web services AWS Java SDK-在EC2实例上使用SSM运行命令
我在网上找不到这方面的任何例子,也找不到解释如何做到这一点的文档。基本上,我有一个WindowsEC2实例的列表,我需要在每个实例中运行Amazon web services AWS Java SDK-在EC2实例上使用SSM运行命令,amazon-web-services,aws-java-sdk,Amazon Web Services,Aws Java Sdk,我在网上找不到这方面的任何例子,也找不到解释如何做到这一点的文档。基本上,我有一个WindowsEC2实例的列表,我需要在每个实例中运行quser命令来检查有多少用户登录 可以使用AWS Systems Manager服务并运行AWS RunPowerShellScript命令来执行此操作。我只找到了使用AWS CLI的示例,如下所示: aws ssm send-command --instance-ids "instance ID" --document-name "AWS-RunPowerS
quser
命令来检查有多少用户登录
可以使用AWS Systems Manager服务并运行AWS RunPowerShellScript命令来执行此操作。我只找到了使用AWS CLI的示例,如下所示:
aws ssm send-command --instance-ids "instance ID" --document-name "AWS-RunPowerShellScript" --comment "Get Users" --parameters commands=quser --output text
但是我如何使用AWS Java SDK 1.11.x实现这一点呢?@Alexandre Krabbe您提出这个问题已经一年多了。所以我不确定答案是否对你有帮助。但我最近也在尝试做同样的事情,这让我想到了一个悬而未决的问题。我最终解决了这个问题,并认为我的答案可以帮助其他面临同样问题的人。下面是一段相同的代码片段:
public void runCommand() throws InterruptedException {
//Command to be run
String ssmCommand = "ls -l";
Map<String, List<String>> params = new HashMap<String, List<String>>(){{
put("commands", new ArrayList<String>(){{ add(ssmCommand); }});
}};
int timeoutInSecs = 5;
//You can add multiple command ids separated by commas
Target target = new Target().withKey("InstanceIds").withValues("instance-id");
//Create ssm client.
//The builder could be chosen as per your preferred way of authentication
//use withRegion for specifying your region
AWSSimpleSystemsManagement ssm = AWSSimpleSystemsManagementClientBuilder.standard().build();
//Build a send command request
SendCommandRequest commandRequest = new SendCommandRequest()
.withTargets(target)
.withDocumentName("AWS-RunShellScript")
.withParameters(params);
//The result has commandId which is used to track the execution further
SendCommandResult commandResult = ssm.sendCommand(commandRequest);
String commandId = commandResult.getCommand().getCommandId();
//Loop until the invocation ends
String status;
do {
ListCommandInvocationsRequest request = new ListCommandInvocationsRequest()
.withCommandId(commandId)
.withDetails(true);
//You get one invocation per ec2 instance that you added to target
//For just a single instance use get(0) else loop over the instanced
CommandInvocation invocation = ssm.listCommandInvocations(request).getCommandInvocations().get(0);
status = invocation.getStatus();
if(status.equals("Success")) {
//command output holds the output of running the command
//eg. list of directories in case of ls
String commandOutput = invocation.getCommandPlugins().get(0).getOutput();
//Process the output
}
//Wait for a few seconds before you check the invocation status again
try {
TimeUnit.SECONDS.sleep(timeoutInSecs);
} catch (InterruptedException e) {
//Handle not being able to sleep
}
} while(status.equals("Pending") || status.equals("InProgress"));
if(!status.equals("Success")) {
//Command ended up in a failure
}
}
public void runCommand()引发InterruptedException{
//要运行的命令
字符串ssmCommand=“ls-l”;
Map params=newhashmap(){{
put(“commands”,newarraylist(){{add(ssmCommand);}});
}};
int timeoutInSecs=5;
//可以添加多个由逗号分隔的命令ID
Target Target=new Target().withKey(“instanceId”).withValues(“实例id”);
//创建ssm客户端。
//可以根据您首选的身份验证方式选择生成器
//使用withRegion指定您的区域
AWSSimpleSystemsManagementSSM=AWSSimpleSystemsManagementClientBuilder.standard().build();
//生成发送命令请求
SendCommandRequest commandRequest=新建SendCommandRequest()
.withTargets(目标)
.withDocumentName(“AWS RunShellScript”)
.带参数(参数);
//结果具有commandId,用于进一步跟踪执行
SendCommandResult commandResult=ssm.sendCommand(commandRequest);
字符串commandId=commandResult.getCommand().getCommandId();
//循环,直到调用结束
字符串状态;
做{
ListCommandInvocationsRequest=新建ListCommandInvocationsRequest()
.withCommandId(commandId)
.详细信息(真实);
//对于添加到目标的每个ec2实例,您将获得一个调用
//对于单个实例,在实例上使用get(0)else循环
CommandInvocation invocation=ssm.listCommandInvocations(请求).getCommandInvocations().get(0);
status=invocation.getStatus();
如果(状态等于(“成功”)){
//命令输出保存运行命令的输出
//例如,ls情况下的目录列表
String commandOutput=invocation.getCommandPlugins().get(0.getOutput();
//处理输出
}
//等待几秒钟,然后再次检查调用状态
试一试{
时间单位。秒。睡眠(timeoutines);
}捕捉(中断异常e){
//处理无法入睡的问题
}
}而(status.equals(“待定”)| status.equals(“InProgress”);
如果(!status.equals(“Success”)){
//命令以失败告终
}
}