简体   繁体   English

Boto 在 ec2 实例上执行 shell 命令

[英]Boto Execute shell command on ec2 instance

I am newbie to EC2 and boto.我是 EC2 和 boto 的新手。 I have an EC2 running instance and I want to execute a shell command like eg apt-get update through boto.我有一个 EC2 运行实例,我想通过 boto 执行一个 shell 命令,例如apt-get update

I searched a lot and found a solution using user_data in the run_instances command, but what if the instance is already launched?我搜索了很多,找到了在run_instances命令中使用user_data的解决方案,但是如果实例已经启动了怎么办?

I don't even know if it is possible.我什至不知道这是否可能。 Any clue in this reference will be a great help.此参考资料中的任何线索都会有很大帮助。

The boto.manage.cmdshell module can be used to do this. boto.manage.cmdshell 模块可用于执行此操作。 To use it, you must have the paramiko package installed.要使用它,您必须安装 paramiko 包。 A simple example of it's use:它的使用的一个简单例子:

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')

That was typed from memory but I think it's correct.那是凭记忆打出来的,但我认为它是正确的。

You could also check out Fabric ( http://docs.fabfile.org/ ) which has similar functionality but also has much more sophisticated features and capabilities.您还可以查看 Fabric ( http://docs.fabfile.org/ ),它具有类似的功能,但还有更复杂的特性和功能。

I think you can use fabric for your requirements.我认为您可以根据您的要求使用面料。 Just check the fabric wrapper once .只需检查一次织物包装纸。 You can execute the command on remote server shell through fabric library.您可以通过fabric 库在远程服务器shell 上执行命令。

It is very easy to use and you can integrate both boto and fabric .它非常易于使用,您可以将 boto 和 fabric 结合起来。 Together they work brilliant.他们一起工作得非常出色。

Plus the same command can executed to n number of nodes.加上可以对 n 个节点执行相同的命令。 Which I believe could be your requirements我相信这可能是您的要求

Just check it out.看看吧。

The easies way is to use kitten python utility which is just a wrapper around boto3.简单的方法是使用小猫python 实用程序,它只是 boto3 的包装器。

eg例如

kitten run "apt-get update" ubuntu 18.105.107.20

Yes, you can do this with AWS Systems manager.是的,您可以使用 AWS 系统管理器执行此操作。 AWS Systems Manager Run Command allows you to remotely and securely run set of commands on EC2 as well on-premise server. AWS Systems Manager Run Command 允许您在 EC2 和本地服务器上远程安全地运行一组命令。 Below are high-level steps to achieve this.以下是实现这一目标的高级步骤。

Attach Instance IAM role: The ec2 instance must have IAM role with policy AmazonSSMFullAccess.附加实例 IAM 角色:ec2 实例必须具有策略为 AmazonSSMFullAccess 的 IAM 角色。 This role enables the instance to communicate with the Systems Manager API.此角色使实例能够与 Systems Manager API 进行通信。

Install SSM Agent: The EC2 instance must have SSM agent installed on it.安装 SSM 代理:EC2 实例上必须安装 SSM 代理。 The SSM Agent process the run command requests & configure the instance as per command. SSM 代理处理运行命令请求并根据命令配置实例。

Execute command : Example usage via AWS CLI:执行命令:通过 AWS CLI 的示例用法:

Execute the following command to retrieve the services running on the instance.执行以下命令以检索在实例上运行的服务。 Replace Instance-ID with ec2 instance id.将 Instance-ID 替换为 ec2 实例 ID。

aws ssm send-command --document-name "AWS-RunShellScript" --comment "listing services" --instance-ids "Instance-ID" --parameters commands="service --status-all" --region us-west-2 --output text

More detailed information: https://www.justdocloud.com/2018/04/01/run-commands-remotely-ec2-instances/更详细的信息: https : //www.justdocloud.com/2018/04/01/run-commands-remotely-ec2-instances/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM