-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendor-cve-checker-json.py
executable file
·75 lines (58 loc) · 1.93 KB
/
vendor-cve-checker-json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import sys
import os
import requests
import json
import logging
def setup_logger(vendor):
# Create a logger
logger = logging.getLogger('CVELogger')
logger.setLevel(logging.INFO)
# Create a file handler for logging
log_filename = f"{vendor}.json"
file_handler = logging.FileHandler(log_filename)
file_handler.setLevel(logging.INFO)
# Create a formatter and set it for the file handler
formatter = logging.Formatter('%(message)s')
file_handler.setFormatter(formatter)
# Add the file handler to the logger
logger.addHandler(file_handler)
return logger
def main():
# Check if API key is set as environment variable
api_key = os.getenv('API_KEY')
if api_key is None:
print("API key not found. Please set API_KEY environment variable.")
return
# Check if vendor name is provided as command line argument
if len(sys.argv) != 2:
print("Usage: python script.py <vendor>")
return
vendor = sys.argv[1]
# Setup logger
logger = setup_logger(vendor)
# Define the URL and parameters
url = "https://services.nvd.nist.gov/rest/json/cves/2.0"
params = {"keywordSearch": vendor}
# Define headers including the apiKey
headers = {
"Accept": "application/json",
"apiKey": api_key
}
# Send the GET request
response = requests.get(url, params=params, headers=headers)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Pretty print the JSON data
log_message = json.dumps(data, indent=4)
print(log_message)
logger.info(log_message)
else:
# Print an error message if the request failed
error_message = f"Error: {response.status_code}"
print(error_message)
logger.error(error_message)
if __name__ == "__main__":
main()