get unused security list from all regions thru boto3
up vote
0
down vote
favorite
i m trying to get unused SG from all regions but its not working .
i tried below code
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs
boto3 aws-security-group
add a comment |
up vote
0
down vote
favorite
i m trying to get unused SG from all regions but its not working .
i tried below code
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs
boto3 aws-security-group
You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i m trying to get unused SG from all regions but its not working .
i tried below code
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs
boto3 aws-security-group
i m trying to get unused SG from all regions but its not working .
i tried below code
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs
boto3 aws-security-group
boto3 aws-security-group
edited Nov 13 at 8:21
mootmoot
5,91822034
5,91822034
asked Nov 11 at 7:33
arushi
75
75
You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00
add a comment |
You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00
You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00
You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
describe_regions is a function of the ec2 client, not the ec2 resource. Try this:
#!/usr/bin/env python
import boto3
ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.
#!/usr/bin/env python
import boto3
ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))
ec2 = boto3.resource('ec2', region_name=reg)
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)
While testing I found I have a lot of unused SGs, thanks.
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
describe_regions is a function of the ec2 client, not the ec2 resource. Try this:
#!/usr/bin/env python
import boto3
ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.
#!/usr/bin/env python
import boto3
ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))
ec2 = boto3.resource('ec2', region_name=reg)
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)
While testing I found I have a lot of unused SGs, thanks.
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
|
show 1 more comment
up vote
0
down vote
describe_regions is a function of the ec2 client, not the ec2 resource. Try this:
#!/usr/bin/env python
import boto3
ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.
#!/usr/bin/env python
import boto3
ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))
ec2 = boto3.resource('ec2', region_name=reg)
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)
While testing I found I have a lot of unused SGs, thanks.
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
describe_regions is a function of the ec2 client, not the ec2 resource. Try this:
#!/usr/bin/env python
import boto3
ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.
#!/usr/bin/env python
import boto3
ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))
ec2 = boto3.resource('ec2', region_name=reg)
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)
While testing I found I have a lot of unused SGs, thanks.
describe_regions is a function of the ec2 client, not the ec2 resource. Try this:
#!/usr/bin/env python
import boto3
ec2 = boto3.client('ec2')
regions = ec2.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
However you're not using the region name in any way. The following code iterates across the regions, sets up a new ec2 resource in each region, and repeats your scans.
#!/usr/bin/env python
import boto3
ec2Client = boto3.client('ec2')
regions = ec2Client.describe_regions().get('Regions',)
for region in regions:
reg=region['RegionName']
print ('Checking region {}'.format(reg))
ec2 = boto3.resource('ec2', region_name=reg)
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print (' Total SGs:', len(all_sgs))
print (' SGS attached to instances:', len(all_inst_sgs))
print (' Orphaned SGs:', len(unused_sgs))
print (' Unattached SG names:', unused_sgs)
While testing I found I have a lot of unused SGs, thanks.
answered Nov 13 at 9:24
weegolo
394
394
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
|
show 1 more comment
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
Thank you so much its working for me as expected and I got the point where i was wrong Thanks again
– arushi
Nov 13 at 12:55
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
hey its working as expected but i need to find unused SGs on basis of EC2,RDS,ELB all for now i am checking only those security group which are not attached to EC2 but i need the list of all SGs which are not attached to any of EC2,RDS,ELB
– arushi
Nov 15 at 12:18
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('rds').describe_db_security_groups()
– weegolo
Nov 16 at 13:00
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
response = boto3.client('elb').describe_load_balancers()
– weegolo
Nov 16 at 13:06
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
Thanks , how can I put my output in one csv file please advise me ?
– arushi
Nov 26 at 6:36
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246720%2fget-unused-security-list-from-all-regions-thru-boto3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You extract the region but not using it anywhere. To switch region in the code, you need to use boto3.session
– mootmoot
Nov 13 at 9:00