Skip to content

Instantly share code, notes, and snippets.

@kjoconnor
Created October 22, 2013 08:50
Show Gist options
  • Save kjoconnor/7097314 to your computer and use it in GitHub Desktop.
Save kjoconnor/7097314 to your computer and use it in GitHub Desktop.
Remove an IP from all EC2 security groups
from boto.ec2 import connect_to_region
ec2 = connect_to_region('us-west-1')
target = '127.0.0.1/32'
remove = False
sgs = ec2.get_all_security_groups()
for sg in sgs:
for rule in sg.rules:
for grant in rule.grants:
if target == grant.cidr_ip:
print "target found in sg {sg}".format(sg=sg.name)
if remove:
print "removing"
sg.revoke(
ip_protocol=rule.ip_protocol,
from_port=rule.from_port,
to_port=rule.from_port,
cidr_ip=target
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment