简体   繁体   English

Python使用Fabric连接到EC2实例

[英]Python using Fabric to connect to an EC2 instance

I'm learning Fabric . 我正在学习Fabric I want to achieve the following: ssh into my EC2 machine and ls the home directory . 我要实现以下目标: ssh进入我的EC2机器并进入主目录

I have started with the following: 我从以下内容开始:

from boto import ec2

from fabric.colors import green as _green, yellow as _yellow


class EC2Conn:
    def __init__(self):
        print(_green("Started..."))
        self.ec2conn = None
        self.user = 'fabUser'
        self.access_key = 'xxxx'
        self.secret_key = 'xxxx'

    def connect(self):
        print(_green("Connecting..."))
        ec2.connect_to_region("eu-west-1a")
        self.ec2conn = ec2.connection.EC2Connection(self.access_key, self.secret_key)

        print(self.get_instances())


    def get_instances(self):
        return self.ec2conn.get_all_instances()


def run_me():
    a = EC2Conn()
    a.connect()

But this gives me a blank list [] I do have 1 instance running so this is incorrect. 但这给了我一个空白列表[]我确实有1个实例在运行,所以这是不正确的。

Try to change little bit in your code as following 尝试如下更改代码中的一些内容

self.ec2conn = ec2.connect_to_region('eu-west-1', 
                  aws_access_key_id=self.access_key,
                  aws_secret_access_key=self.secret_key)

print(self.get_instances())

Or 要么

region = ec2.get_region('eu-west-1')
self.ec2conn = ec2.connection.EC2Connection(self.access_key,
                  self.secret_key, region=region)

print(self.get_instances())

Remember that ec2.connect_to_region and ec2.connection.EC2Connection both returns ec2.connection.EC2Connection object. 请记住, ec2.connect_to_regionec2.connection.EC2Connection都返回ec2.connection.EC2Connection对象。 Refer here 请参考这里

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

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