TerminalDilettante
TerminalDilettante

Reputation: 615

How to tell if AWS Elastic Network Interface (ENI) has a public IP address using Python boto?

I am auditing security groups and right now my task is to find ENIs with no public IP addresses with inbound security group rules that reference public IP addresses.

I have code that iterates over ENIs and looks at the Security Groups attached to each one and I'm hoping to recycle that. Here's that code:

used_sgs = {}
enis = ec2_conn.get_all_network_interfaces()
for eni in enis:
    eni_sgs = eni.groups
    for sg in eni_sgs:
        sgn = sg.name
        if sgn not in used_sgs:
           used_sgs[sgn] = 0
        used_sgs[sgn]+=1

Is there an attribute I can sniff to see if an ENI has a public IP? Or do I have to take some other route (iterate over instances, or grab the ENI attachment, or something else)?

Upvotes: 0

Views: 1108

Answers (1)

helloV
helloV

Reputation: 52393

eni.publicIp

Not all enis have publicIp associated with them. So check if publicIp attribute exists and then get that value

for eni in enis:
  if hasattr(eni, 'publicIp') and eni.publicIp:
    print eni.id, eni.publicIp

Upvotes: 1

Related Questions