Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
Updating shodan initialization function
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna committed Jun 21, 2023
1 parent 1475518 commit 0a63cf5
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/pe_source/data/pe_db/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,28 @@ def shodan_api_init():
"""Connect to Shodan API."""
section = "shodan"
api_list = []
if os.path.isfile(REPORT_DB_CONFIG):
if not os.path.isfile(REPORT_DB_CONFIG):
raise Exception(f"Database.ini file not found at this path: {REPORT_DB_CONFIG}")

with open(REPORT_DB_CONFIG, encoding="utf-8") as config_file:
parser = ConfigParser()
parser.read(REPORT_DB_CONFIG, encoding="utf-8")
if parser.has_section(section):
params = parser.items(section)
else:
parser.read_file(config_file)
if not parser.has_section(section):
raise Exception(
"Section {} not found in the {} file".format(section, REPORT_DB_CONFIG)
f"Section {section} not found in the {REPORT_DB_CONFIG} file"
)
else:
raise Exception(
"Database.ini file not found at this path: {}".format(REPORT_DB_CONFIG)
)
params = parser.items(section)

for key in params:
for key, value in params:
try:
api = shodan.Shodan(key[1])
api = shodan.Shodan(value)
# Test api key
api.info()
except Exception:
LOGGER.error("Invalid Shodan API key: {}".format(key))
continue
api_list.append(api)
LOGGER.info("Number of valid Shodan API keys: {}".format(len(api_list)))
api_list.append(api)
except shodan.APIError as e:
LOGGER.error(f"Invalid Shodan API key: {key} ({e})")

LOGGER.info(f"Number of valid Shodan API keys: {len(api_list)}")
return api_list


Expand Down

0 comments on commit 0a63cf5

Please sign in to comment.