【问题标题】:Boto3: create, execute shell command, terminate on ec2 instanceBoto3:创建,执行shell命令,在ec2实例上终止
【发布时间】:2016-12-09 20:08:07
【问题描述】:

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

我搜索了很多,并找到了 botoparamiko 的方法。我不知道,是否可以使用 boto3 在 ec2 实例中运行 shell 脚本。本参考资料中的任何线索或示例都会有很大帮助。

谢谢!

【问题讨论】:

    标签: python shell amazon-ec2 boto3


    【解决方案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')
    

    【讨论】:

      【解决方案2】:

      Boto 和 Boto3 之间是有区别的。问题指定了 boto,但问题标题显示的是 Boto3。

      接受的答案对于 boto 是正确的。但是,对于 boto3,这个问题得到了回答 here :)

      【讨论】:

        猜你喜欢
        • 2013-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-12
        • 2019-02-19
        • 2018-07-12
        相关资源
        最近更新 更多