monthly-report-gos4.gmp   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 6

3 Functions

Rating   Name   Duplication   Size   Complexity  
B print_reports() 0 57 3
A main() 0 13 1
A check_args() 0 20 2
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2017-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import sys
20
from datetime import date, timedelta
21
from terminaltables import AsciiTable
22
23
24
def check_args(args):
25
    len_args = len(args.script) - 1
26
    if len_args < 2:
27
        message = """
28
        This script will display all vulnerabilities from the hosts of the
29
        reports in a given month!
30
        It needs two parameters after the script name.
31
        First one is the month and second one is the year.
32
        Both parameters are plain numbers, so no text.
33
        Explicitly made for GOS 4.X.
34
35
        1. <month>  -- month of the monthly report
36
        2. <year>   -- year of the monthly report
37
38
        Example:
39
            $ gvm-script --gmp-username name --gmp-password pass \
40
    ssh --hostname <gsm> scripts/monthly-report2.gmp.py 05 2019
41
        """
42
        print(message)
43
        sys.exit()
44
45
46
def print_reports(gmp, from_date, to_date):
47
    asset_filter = "rows=-1 and modified>{0} and modified<{1}".format(
48
        from_date.isoformat(), to_date.isoformat()
49
    )
50
51
    assets_xml = gmp.get_assets(
52
        asset_type=gmp.types.AssetType.HOST, filter=asset_filter
53
    )
54
55
    sum_high = 0
56
    sum_medium = 0
57
    sum_low = 0
58
    table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']]
59
60
    for asset in assets_xml.xpath('asset'):
61
        ip = asset.xpath('name/text()')[0]
62
63
        hostnames = asset.xpath(
64
            'identifiers/identifier/name[text()="hostname"]/../value/text()'
65
        )
66
67
        if len(hostnames) == 0:
68
            continue
69
70
        hostname = hostnames[0]
71
72
        results = gmp.get_results(
73
            details=False, filter='host={0} and severity>0.0'.format(ip)
74
        )
75
76
        low = int(results.xpath('count(//result/threat[text()="Low"])'))
77
        sum_low += low
78
79
        medium = int(results.xpath('count(//result/threat[text()="Medium"])'))
80
        sum_medium += medium
81
82
        high = int(results.xpath('count(//result/threat[text()="High"])'))
83
        sum_high += high
84
85
        best_os_cpe_report_id = asset.xpath(
86
            'host/detail/name[text()="best_os_cpe"]/../source/@id'
87
        )[0]
88
89
        table_data.append(
90
            [hostname, ip, best_os_cpe_report_id, high, medium, low]
91
        )
92
93
    table = AsciiTable(table_data)
94
    print(table.table + '\n')
95
    print(
96
        'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}'
97
        '\nLow: {2}\n\n'.format(
98
            int(sum_high),
99
            int(sum_medium),
100
            int(sum_low),
101
            from_date.isoformat(),
102
            to_date.isoformat(),
103
        )
104
    )
105
106
107
def main(gmp, args):
108
    # pylint: disable=undefined-variable
109
110
    check_args(args)
111
112
    month = int(args.script[1])
113
    year = int(args.script[2])
114
    from_date = date(year, month, 1)
115
    to_date = from_date + timedelta(days=31)
116
    # To have the first day in month
117
    to_date = to_date.replace(day=1)
118
119
    print_reports(gmp, from_date, to_date)
120
121
122
if __name__ == '__gmp__':
123
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
124