【发布时间】:2016-03-10 01:14:02
【问题描述】:
Python 新手.. 我正在尝试编写一个脚本,该脚本将使用我提供的密钥连接到我的 AWS 账户,然后它将列出其中的实例,并通过遍历每个区域来实现。所以我希望它连接并在一个区域和列表中开始,然后转到下一个区域和列表,依此类推。 如果我注释掉以下行,则下面的代码有效,但它仅适用于 us-east-1,这是我在我的盒子上的 AWS 凭证文件中的默认区域。 “区域 = boto.ec2.regions() 对于区域中的 x:" 如果我取消注释这些行,脚本将为每个区域返回一次我的所有实例。因此,我最终得到了大量重复的相同实例列表。我错过了什么会让这做我想要的?很简单,我对 Python 很陌生。 谢谢。
import boto.ec2
import os
import sys
ACCESS_KEY = raw_input("Enter your access key > ")
SECRET_KEY = raw_input("Enter your secret key > ")
if not ACCESS_KEY:
sys.exit("ERROR: You did not enter anything for ACCESS KEY or SECRET KEY. Exiting...")
ec2_conn = boto.connect_ec2(ACCESS_KEY, SECRET_KEY)
regions = boto.ec2.regions()
for x in regions:
reservations = ec2_conn.get_all_reservations()
for r in reservations:
for i in r.instances:
print r.instances
print 'Tags: ',i.tags['Name']
print 'Public IP Address: ',i.ip_address
print 'Private IP address: ',i.private_ip_address
if (i.virtualization_type == 'hvm'):
platform = 'Windows'
else:
platform = 'Linux'
print 'Platform: ',platform
print 'State: ',i.state
print
【问题讨论】:
标签: python amazon-web-services amazon-ec2 boto