1

I am trying to understand more on Boto3 scripting.

I want to search for unused security groups within several VPC's which are all in the same region

I am trying to get the python script here to work: boto3 searching unused security groups

So my list-unused-sq.py is shown below

import boto3

ec2 = boto3.resource('ec2')

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

When i run the script i get the following error

./list-unused-sq.py: line 1: import: command not found
./list-unused-sq.py: line 3: syntax error near unexpected token `('
./list-unused-sq.py: line 3: `ec2 = boto3.resource('ec2') #You have to change this line based on how you pass AWS credentials and AWS config'

Is someone able to point out where i have gone wrong and what i need to do to correct it?

Thanks Nick

Nick
  • 23
  • 4

1 Answers1

1

Look at your first error line:

./list-unused-sq.py: line 1: import: command not found    

Seems like your problem is not related with boto3 but in your script not recognizing your local python. More info about your problem and how to solve it

SpKel
  • 542
  • 5
  • 15
  • Thank you very much. I really appreciate you replying and that was exactly what solved my issue! . . `Total SGs: 78 SGS attached to instances: 30 Orphaned SGs: 48 Unattached SG names: set(['launch-wiz` – Nick Jun 23 '17 at 11:48