2016-08-03 145 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 :)