2014-03-04 32 views
1

我在學習結構。我想實現以下:ssh到我的EC2機器和ls的主目錄Python使用結構連接到EC2實例

我已經開始有以下:

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

但是,這給了我一個空白列表[]我有1個實例中運行的,所以這是不正確。

+0

它顯示任何錯誤? –

+0

沒有錯誤,它與細節連接,但顯示並清空[]。當它應該有2個實例。我設置了一個新用戶並賦予它們正確的權限(完全管理) – Prometheus

回答

2

嘗試改變點點在你的代碼如下

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

或者

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

print(self.get_instances()) 

記住ec2.connect_to_regionec2.connection.EC2Connection都返回ec2.connection.EC2Connection對象。請參考here

+0

頂級代碼給我NoneType'對象沒有屬性'get_all_instances',第二個沒有相同的[]空白 – Prometheus

+0

你檢查了區域嗎?這是對的嗎?用aws控制檯檢查它。 –

+0

在控制檯中,它聲明實例的可用區域是eu-west-1a。 – Prometheus