Python Boto3:创建、执行shell命令,在ec2实例上终止
我是EC2和boto的新手。我必须创建一个运行EC2的实例,在那里我可以用S3文件发送命令并执行shell脚本 我搜索了很多,找到了一种与boto和paramiko有关的方法。我不知道,是否可以在ec2实例中使用boto3运行shell脚本。本参考资料中的任何线索或示例都将非常有帮助Python Boto3:创建、执行shell命令,在ec2实例上终止,python,shell,amazon-ec2,boto3,Python,Shell,Amazon Ec2,Boto3,我是EC2和boto的新手。我必须创建一个运行EC2的实例,在那里我可以用S3文件发送命令并执行shell脚本 我搜索了很多,找到了一种与boto和paramiko有关的方法。我不知道,是否可以在ec2实例中使用boto3运行shell脚本。本参考资料中的任何线索或示例都将非常有帮助 谢谢大家! 可以使用boto.manage.cmdshell模块执行此操作。要使用它,必须安装paramiko软件包。一个简单的使用示例: import boto.ec2 from boto.manage.cmds
谢谢大家! 可以使用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')
导入boto.ec2
从boto.manage.cmdshell从\u实例导入sshclient\u
#连接到您选择的地区
conn=boto.ec2.将_连接到_地区(“us-west-2”)
#查找与我的instanceId相关的实例对象
instance=conn.get_all_实例(['i-12345678'])[0]。实例[0]
#为我们的实例创建一个SSH客户端
#key_path是与实例关联的SSH私钥的路径
#user_name是作为实例登录的用户(例如ubuntu、ec2用户等)
ssh_client=sshclient_from_实例(实例,
'',
user_name='ec2-user')
#运行命令。返回由以下内容组成的元组:
#命令的整数状态
#包含命令输出的字符串
#包含命令的stderr输出的字符串
status,stdout,stderr=ssh_client.run('ls-al'))
可以使用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')
导入boto.ec2
从boto.manage.cmdshell从\u实例导入sshclient\u
#连接到您选择的地区
conn=boto.ec2.将_连接到_地区(“us-west-2”)
#查找与我的instanceId相关的实例对象
instance=conn.get_all_实例(['i-12345678'])[0]。实例[0]
#为我们的实例创建一个SSH客户端
#key_path是与实例关联的SSH私钥的路径
#user_name是作为实例登录的用户(例如ubuntu、ec2用户等)
ssh_client=sshclient_from_实例(实例,
'',
user_name='ec2-user')
#运行命令。返回由以下内容组成的元组:
#命令的整数状态
#包含命令输出的字符串
#包含命令的stderr输出的字符串
status,stdout,stderr=ssh_client.run('ls-al'))
Boto和Boto3之间有区别。该问题指定boto,但问题标题为Boto3
接受的答案对boto是正确的。但是,对于boto3,这个问题得到了回答:)Boto和boto3之间有区别。该问题指定boto,但问题标题为Boto3 接受的答案对boto是正确的。但是,对于boto3来说,这个问题得到了回答:)