Skip to content

Commit

Permalink
Update main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
IdanR-lighspin authored Nov 12, 2021
1 parent 7d8c484 commit 6f4d853
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
from art import text2art
import random
import boto3
import os
import glob
from src.logger import setup_logger
from src.snapper import Snapper
from src.scanner import Scanner


def getting_all_pem_file_names():
"""
:return: .pem file names from the red-detector directory.
"""
file_path = os.path.realpath(__file__) # getting the script's path
file_path = file_path.split("red-detector")
files_path = file_path[0] + "red-detector" # (the pem files arent in the same directory as the script.)

lst = (glob.glob(files_path+"/*.pem"))
index = 0
for i in lst:
lst[index] = lst[index].replace(files_path+"/", "").replace(".pem","")
index += 1
return lst


def used_key_pairs():
keypairs = [] # list of used keyPair names
ec2 = boto3.client('ec2')
Expand Down Expand Up @@ -52,14 +70,14 @@ def used_key_pairs():
if cmd_args.keypair:
scanner = Scanner(logger=logger, region=snapper.region, key_pair_name=cmd_args.keypair)
else:
used_key_pairs_list = used_key_pairs()
used_key_pairs_list_from_aws = used_key_pairs()
used_key_pairs_list_locally = getting_all_pem_file_names()
num = 0
key_name = "red_detector_key{number}".format(number=str(num))
while key_name in used_key_pairs_list:
while key_name in used_key_pairs_list_from_aws or key_name in used_key_pairs_list_locally:
num += 1
key_name = "red_detector_key{number}".format(number=str(num))


scanner = Scanner(logger=logger, region=snapper.region, key_pair_name=key_name)
scanner.keypair_name = scanner.create_keypair(key_name=key_name)

Expand Down

1 comment on commit 6f4d853

@IdanR-lighspin
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a little bug. I wrote a description in OS-47 in jira.

Please sign in to comment.