我需要从AWS精确地列出所有正在运行的IP,并且在IP列表中我还需要标记值& region-name(或availability-zone)。该列表应如下所示
ex : [[('23.1.1.141', ' Production LB', 'us-east-1'), ('47.2.14.93', 'sung2-cloud-trial01-LBi-b89671', 'us-west-2'),................]]
该清单包括以下内容:
<'Running IP'>,<'Tag-value'>,<AvailabilityZone>
使用boto3
,我可以使用describe_instances()
实现此目的,因此我尝试按如下方式使用它:
def gather_public_ip():
regions = ['us-west-2', 'eu-central-1', 'ap-southeast-1']
combined_list = [] ##This needs to be returned
for region in regions:
instance_information = {}
ip_dict = {}
client = boto3.client('ec2', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY,
region_name=region, )
instance_dict = client.describe_instances().get('Reservations')
for instance in instance_dict:
.....
#WHAT CODE COMES HERE???
gather_public_ip()
那么究竟应该为此寻找什么呢?我不知道该怎么做,请帮忙
新的回应:
Traceback (most recent call last):
File "C:/Users/nisingh/PycharmProjects/untitled1/more_specific.py", line 35, in <module>
gather_public_ip()
File "C:/Users/nisingh/PycharmProjects/untitled1/more_specific.py", line 27, in gather_public_ip
ipaddress = instance['PublicIpAddress'] # Which one do you need?
KeyError: 'PublicIpAddress'
{u'Monitoring': {u'State': 'disabled'}, u'PublicDnsName': '', u'RootDeviceType': 'ebs', u'State': {u'Code': 80, u'Name': 'stopped'}, u'EbsOptimized': False, u'LaunchTime': datetime.datetime(2015, 5, 2, 16, 34, 17, tzinfo=tzutc()), u'PrivateIpAddress': '10.11.32.10', u'ProductCodes': [], u'VpcId': 'vpc-8ae766ef', u'StateTransitionReason': 'User initiated (2016-01-29 22:20:18 GMT)', u'InstanceId': 'i-5e8ae8a8', u'ImageId': 'ami-5189a661', u'PrivateDnsName': 'ip-10-11-32-10.us-west-2.compute.internal', u'KeyName': 'pdx01-cump-050215', u'SecurityGroups': [{u'GroupName': 'pdx01-clv-jumpeng', u'GroupId': 'sg-50abbf35'}], u'ClientToken': 'inYin1430584456904', u'SubnetId': 'subnet-57e45b20', u'InstanceType': 't2.micro', u'NetworkInterfaces': [{u'Status': 'in-use', u'MacAddress': '06:91:7c:86:7c:d5', u'SourceDestCheck': True, u'VpcId': 'vpc-8ae766ef', u'Description': 'Primary network interface', u'NetworkInterfaceId': 'eni-7d566d0b', u'PrivateIpAddresses': [{u'Primary': True, u'PrivateIpAddress': '10.11.32.10'}], u'Attachment': {u'Status': 'attached', u'DeviceIndex': 0, u'DeleteOnTermination': True, u'AttachmentId': 'eni-attach-08d40929', u'AttachTime': datetime.datetime(2015, 5, 2, 16, 34, 17, tzinfo=tzutc())}, u'Groups': [{u'GroupName': 'pdx01-cloud-dev-jumpeng', u'GroupId': 'sg-50abbf35'}], u'SubnetId': 'subnet-57e45b20', u'OwnerId': '547316675291', u'PrivateIpAddress': '10.11.32.10'}], u'SourceDestCheck': True, u'Placement': {u'Tenancy': 'default', u'GroupName': '', u'AvailabilityZone': 'us-west-2b'}, u'Hypervisor': 'xen', u'BlockDeviceMappings': [{u'DeviceName': '/dev/sda1', u'Ebs': {u'Status': 'attached', u'DeleteOnTermination': True, u'VolumeId': 'vol-0990f21b', u'AttachTime': datetime.datetime(2015, 5, 2, 16, 34, 21, tzinfo=tzutc())}}], u'Architecture': 'x86_64', u'StateReason': {u'Message': 'Client.UserInitiatedShutdown: User initiated shutdown', u'Code': 'Client.UserInitiatedShutdown'}, u'RootDeviceName': '/dev/sda1', u'VirtualizationType': 'hvm', u'Tags': [{u'Value': 'pdx01-jump02-DEPRECATED', u'Key': 'Name'}, {u'Value': 'Cloud Eng Jump Box', u'Key': 'Description'}], u'AmiLaunchIndex': 0}
答案 0 :(得分:2)
我认为这完全取决于在回复中找到信息并抓住它;
def gather_public_ip():
regions = ['us-west-2', 'eu-central-1', 'ap-southeast-1']
combined_list = [] ##This needs to be returned
for region in regions:
instance_information = [] # I assume this is a list, not dict
ip_dict = {}
client = boto3.client('ec2', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY,
region_name=region, )
instance_dict = client.describe_instances().get('Reservations')
for reservation in instance_dict:
for instance in reservation['Instances']: # This is rather not obvious
if instance[unicode('State')][unicode('Name')] == 'running':
try:
ipaddress = instance[unicode('PublicIpAddress')]
except:
break
tagValue = instance[unicode('Tags')][0][unicode('Value')] # 'Tags' is a list, took the first element, you might wanna switch this
zone = instances[unicode('Placement')][unicode('AvailabilityZone')]
info = ipaddress, tagValue, zone
instance_information.append(info)
combined_list.append(instance_information)
return combined_list
gather_public_ip()
我希望这是您正在寻找的内容,我也希望您不要直接复制,但请查看我如何从文档中的返回dict
中提取信息。
答案 1 :(得分:0)
尝试ip_addr = instance_dict['Reservations'][0]['Instances'][0]['PublicIpAddress']
来获取IP地址。 describe_instaces()
响应是一个字典,其值是包含多个其他字典的数组,依此类推。...