<p>这条线索有点老了,但由于我花了一个令人沮丧的下午发现了一个简单的解决方案,我不妨分享一下。</p>
<p>注意这不是对OP问题的严格回答,因为它不使用ssh。但是,boto3的一点是你不必-所以我认为在大多数情况下,这将是实现OP目标的首选方法,因为s/他可以简单地使用他/她现有的boto3配置。</p>
<p>AWS的Run命令内置于botocore中(据我所知,这应该同时适用于boto和boto3),但免责声明:<em>我只使用boto3</em>进行了测试。</p>
<pre><code>def execute_commands_on_linux_instances(client, commands, instance_ids):
"""Runs commands on remote linux instances
:param client: a boto/boto3 ssm client
:param commands: a list of strings, each one a command to execute on the instances
:param instance_ids: a list of instance_id strings, of the instances on which to execute the command
:return: the response from the send_command function (check the boto3 docs for ssm client.send_command() )
"""
resp = client.send_command(
DocumentName="AWS-RunShellScript", # One of AWS' preconfigured documents
Parameters={'commands': commands},
InstanceIds=instance_ids,
)
return resp
# Example use:
ssm_client = boto3.client('ssm') # Need your credentials here
commands = ['echo "hello world"']
instance_ids = ['an_instance_id_string']
execute_commands_on_linux_instances(ssm_client, commands, instance_ids)
</code></pre>
<p>对于windows实例powershell命令,您可以使用另一个选项:</p>
<pre><code> DocumentName="AWS-RunPowerShellScript",
</code></pre>