【发布时间】:2017-07-27 11:28:27
【问题描述】:
我希望能够通过 ssh 进入 EC2 实例,并在其中运行一些 shell 命令,例如 this。
如何在 boto3 中做到这一点?
【问题讨论】:
标签: python amazon-ec2 boto3
我希望能够通过 ssh 进入 EC2 实例,并在其中运行一些 shell 命令,例如 this。
如何在 boto3 中做到这一点?
【问题讨论】:
标签: python amazon-ec2 boto3
Boto 提供了一种使用 Paramiko 以编程方式通过 SSH 连接到 EC2 实例然后运行命令的方法。 Boto3 不包含此功能。您可以修改 boto 代码以使用 boto3 而不需要大量的努力。或者您可以考虑使用类似 fabric 或 ansible 的东西,它们提供了一种更强大的方式来远程在 EC2 实例上执行命令。
【讨论】:
您可以使用以下代码 sn-p ssh 到 EC2 实例并从 boto3 运行一些命令。
import boto3
import botocore
import paramiko
key = paramiko.RSAKey.from_private_key_file(path/to/mykey.pem)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect/ssh to an instance
try:
# Here 'ubuntu' is user name and 'instance_ip' is public IP of EC2
client.connect(hostname=instance_ip, username="ubuntu", pkey=key)
# Execute a command(cmd) after connecting/ssh to an instance
stdin, stdout, stderr = client.exec_command(cmd)
print stdout.read()
# close the client connection once the job is done
client.close()
break
except Exception, e:
print e
【讨论】:
import boto3或import botocore的理由吗?
这个帖子有点老了,但由于我花了一个令人沮丧的下午发现一个简单的解决方案,我不妨分享一下。
NB 这不是对 OP 问题的严格回答,因为它不使用 ssh。但是,boto3 的一点是您不必这样做 - 所以我认为在大多数情况下,这将是实现 OP 目标的首选方式,因为他/她可以轻松地使用他/她现有的 boto3 配置。
AWS 的运行命令内置于 botocore 中(因此据我所知,这应该适用于 boto 和 boto3)但免责声明:我只使用 boto3 进行了测试。
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)
对于 Windows 实例 powershell 命令,您可以使用替代选项:
DocumentName="AWS-RunPowerShellScript",
【讨论】:
print(instance_ids) 进行检查]或者您提供的实例 ID 与正在运行的机器实例不对应[登录到您的 AWS 控制台以检查您指定的机器实例是否实际运行]。如果这些检查失败,我建议发布一个新的 SO问题:)
使用 boto3 发现实例并使用 fabric 在实例上运行命令
【讨论】:
这是我的做法
import boto3
import botocore
import boto
import paramiko
ec2 = boto3.resource('ec2')
instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
i = 0
for instance in instances:
print(instance.id, instance.instance_type)
i+= 1
x = int(input("Enter your choice: "))
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
privkey = paramiko.RSAKey.from_private_key_file('address to .pem key')
ssh.connect(instance.public_dns_name,username='ec2-user',pkey=privkey)
stdin, stdout, stderr = ssh.exec_command('python input_x.py')
stdin.flush()
data = stdout.read().splitlines()
for line in data:
x = line.decode()
#print(line.decode())
print(x,i)
ssh.close()
对于credentails,我已经添加了AWSCLI包,然后在终端运行
aws configure
输入凭据。它们都将保存在 .aws 文件夹中,您也可以更改路径。
【讨论】:
您也可以使用kittenpython 库来实现它,它只是对 boto3 的封装。您还可以使用此实用程序同时在多台服务器上运行相同的命令。
例如。
kitten run uptime ubuntu 18.105.107.20
【讨论】: