简体   繁体   English

Python Boto EC2根据其IP地址查找实例

[英]Python Boto EC2 find instance given its IP address

在Python中使用boto,如何在给定IP地址的情况下找到boto.ec2实例对象?

Digging through the boto documentation , I found the get_only_instances method, which you use to get all instances. 通过boto文档 ,我找到了get_only_instances方法,用于获取所有实例。 You can pass a filter dictionary to it, to filter by IP Address (I found this in the EC2 API Reference under the Filter.N title). 您可以将过滤字典传递给它,按IP地址过滤(我在Filter.N标题下的EC2 API Reference中找到了这个)。

So for example, to get the instance with IP 1.1.1.1 , you would do: 例如,要获得IP 1.1.1.1的实例,您可以:

filters = {"ip-address": "1.1.1.1"}
result_list = conn.get_only_instances(filters=filters)

Then result_list[0] should be the Instance object for the instance with that IP address. 然后result_list[0]应该是具有该IP地址的实例的Instance对象。

boto3 boto3

ec2 = boto3.client('ec2')

filters = [{
    'Name': 'ip-address', 
    'Values': ['1.1.1.1'],
}]
result_list = ec2.describe_instances(Filters=filters)

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

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