Created
October 22, 2013 08:50
-
-
Save kjoconnor/7097314 to your computer and use it in GitHub Desktop.
Remove an IP from all EC2 security groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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