2016-08-03 138 views
0

我是新手EC2boto。我必须创建一个EC2运行实例,我可以在其中发送带有S3文件的命令并执行一个shell脚本。Boto3:创建,执行shell命令,在ec2实例上终止

我搜查了很多,并找到一种方式botoparamiko。我不知道,是否可以使用boto3在ec2实例中运行shell脚本。此参考文献中的任何线索或示例都将非常有帮助。

谢谢!

回答

1

boto.manage.cmdshell模块可用于执行此操作。要使用它,你必须安装paramiko软件包。一个简单的例子:

import boto.ec2 
from boto.manage.cmdshell import sshclient_from_instance 

# Connect to your region of choice 
conn = boto.ec2.connect_to_region('us-west-2') 

# Find the instance object related to my instanceId 
instance = conn.get_all_instances(['i-12345678'])[0].instances[0] 

# Create an SSH client for our instance 
# key_path is the path to the SSH private key associated with instance 
# user_name is the user to login as on the instance (e.g. ubuntu, ec2-user, etc.) 
ssh_client = sshclient_from_instance(instance, 
            '<path to SSH keyfile>', 
            user_name='ec2-user') 
# Run the command. Returns a tuple consisting of: 
# The integer status of the command 
# A string containing the output of the command 
# A string containing the stderr output of the command 
status, stdout, stderr = ssh_client.run('ls -al') 
0

Boto和Boto3是有区别的。问题指定boto,但问题标题说Boto3。

接受的答案是正确的博托。但是,对于boto3,这个问题的答案是here :)