Sobes.tech
Junior — Middle+

Identification of vulnerable devices by CVE

livecode

Task condition

It is necessary to build a data structure in which each device identifier (device_id) is associated with the number of vulnerabilities found for it. Below is an example implementation that sequentially requests API pages, counts the number of vulnerabilities in each response, and forms the final dictionary as well as the total number of vulnerabilities.


def process_paginated_response(content, res):
    total = 0
    for item in content:
        device_id = item["device_id"]
        vuln_num = len(item["vulnerabilities"])
        res[device_id] += vuln_num
        total += vuln_num
    return total

res = defaultdict(int)
total_ = 0
page_number = 1

while True:
    url = f"{base_url}?page_number={page_number}"
    content_ = requests.get(url, auth=(username, password)).json()
    total_ += process_paginated_response(content_["results"], res)
    if content_["more"] is False:
        break
    page_number += 1

print(json.dumps(dict(res), indent=4))
print(total_)