如何使用Boto3在AWS Instance上执行命令

时间:2015-12-01 19:12:28

标签: python amazon-web-services boto3

任何人都可以告诉我是否可以在已发布的AWS实例上使用Boto3执行Shell命令。

我在几个地方读过“boto.manage.cmdshell”,但在Boto3中已弃用。

感谢任何帮助。

此致 SAURABH

6 个答案:

答案 0 :(得分:6)

ssm_client = boto3.client('ssm')
response = ssm_client.send_command(
            InstanceIds=['i-03#####'],
            DocumentName="AWS-RunShellScript",
            Parameters={'commands': ['start ecs']}, )

command_id = response['Command']['CommandId']
output = ssm_client.get_command_invocation(
      CommandId=command_id,
      InstanceId='i-03######',
    )
print(output)

答案 1 :(得分:3)

ssm = boto3.client('ssm' )    
testCommand = ssm.send_command( InstanceIds=[ 'i-123123123123' ], DocumentName='AWS-RunShellScript', Comment='la la la', OutputS3BucketName='myOutputS3Bucket', OutputS3KeyPrefix='i-123123123123', Parameters={ "commands":[ "ip config" ]  } )

i-123123123123是假装的ec2实例ID。 我把它放在OutputS3KeyPrefix中,以获得一个独特的位置来存储桶中的日志。

您可以像这样安装ssm代理;

ec2r = boto3.resource('ec2' )
userdata = """#cloud-config
    runcmd:
     - /home/ec2-user/sudo npm run prod
     - cd /tmp
     - curl https://amazon-ssm-%s.s3.amazonaws.com/latest/linux_amd64/amazon-ssm-agent.rpm -o amazon-ssm-agent.rpm
     - yum install -y amazon-ssm-agent.rpm
""" % region   

if ssm == "on":
    instance = ec2r.create_instances( ImageId=ami, MinCount=1, MaxCount=1, KeyName=keyname, InstanceType=instancetype, 
        NetworkInterfaces=[{
        'DeviceIndex': 0,
        'AssociatePublicIpAddress': False,
        'SubnetId': mySub,
        'Groups': secGroupList,
        'AssociatePublicIpAddress': AssociatePublicIpAddress
    }],
        Monitoring={ 'Enabled': False },

        UserData=userdata,
        IamInstanceProfile={
            'Name': rolename
        },
        EbsOptimized=False
    )

答案 2 :(得分:2)

没有。 boto中的boto.manage.cmdshell功能未迁移到boto3。原始的boto.manage.cmdshell功能使用了Paramiko,如果您想使用boto3的SSH功能,可以直接使用boto3。

这是关于此主题的boto3 github issue

@jarmod指出截至2015年10月有new AWS functionality,您可以使用AWS EC2 SSM在Windows系统上运行命令。您可以使用boto3 SSM client从botocore版本1.3.1开始在boto3中访问它。

这是支持“EC2 Run Command”的boto3 github issue

答案 3 :(得分:1)

更改

command_id = response['Command']['CommandId']

command_id = context.aws_request_id

答案 4 :(得分:0)

我知道我正在回答有点老话题。我不确定即使在那个时候SSM存在。但现在您可以使用boto3中的SSM send_command直接在ec2实例上运行命令。 以下是在EC2实例上运行PowerShell命令的示例

import boto3
ssm_client = boto3.client('ssm', region_name="us-west-2") # use region code in which you are working
response = ssm_client.send_command(
             InstanceIds=[
                "i-03########" # use instance id on which you want to execute, even multiple is allowd
                     ],
             DocumentName="AWS-RunPowerShellScript",
             Parameters={
                'commands':[
                     'ipconfig'
                       ]
                   },
             })
command_id = response['Command']['CommandId']
output = ssm_client.get_command_invocation(
      CommandId=command_id,
      InstanceId='i-03######',
    )
print(output)

有关详细信息,请参阅boto3 SSM docs 有关SSM本身的信息,请参阅AWS文档

答案 5 :(得分:0)

Documentation说:

  

aws_request_id

     

与请求关联的AWS请求ID。这是返回给调用了invoke方法的客户端的ID。

更改:

command_id = response['Command']['CommandId']

针对:

command_id = context.aws_request_id
相关问题