Passed
Pull Request — master (#244)
by
unknown
01:35
created

monthly-report2.gmp   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 10.57 %

Importance

Changes 0
Metric Value
eloc 58
dl 13
loc 123
rs 10
c 0
b 0
f 0
wmc 6

3 Functions

Rating   Name   Duplication   Size   Complexity  
B print_reports() 0 57 3
A check_args() 0 20 2
A main() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2017-2019 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
from datetime import date, timedelta
20
from terminaltables import AsciiTable
21
22
23
def check_args(args):
24
    len_args = len(args.script) - 1
25
    if len_args < 2:
26
        message = """
27
        This script will display all vulnerabilities from the hosts of the
28
        reports in a given month!
29
        It needs two parameters after the script name.
30
        First one is the month and second one is the year.
31
        Both parameters are plain numbers, so no text.
32
        Explicitly made for GOS 4.X.
33
34
        1. <month>  -- month of the monthly report
35
        2. <year>   -- year of the monthly report
36
37
        Example:
38
            $ gvm-script --gmp-username name --gmp-password pass \
39
    ssh --hostname <gsm> scripts/monthly-report2.gmp.py 05 2019
40
        """
41
        print(message)
42
        quit()
43
44
45
def print_reports(gmp, from_date, to_date):
46
    asset_filter = "rows=-1 and modified>{0} and modified<{1}".format(
47
        from_date.isoformat(), to_date.isoformat()
48
    )
49
50
    assets_xml = gmp.get_assets(
51
        asset_type=gmp.types.AssetType.HOST, filter=asset_filter
52
    )
53
54
    sum_high = 0
55
    sum_medium = 0
56
    sum_low = 0
57
    table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']]
58
59
    for asset in assets_xml.xpath('asset'):
60
        ip = asset.xpath('name/text()')[0]
61
62
        hostnames = asset.xpath(
63
            'identifiers/identifier/name[text()="hostname"]/../value/text()'
64
        )
65
66
        if len(hostnames) == 0:
67
            continue
68
69
        hostname = hostnames[0]
70
71
        results = gmp.get_results(
72
            details=False, filter='host={0} and severity>0.0'.format(ip)
73
        )
74
75
        low = int(results.xpath('count(//result/threat[text()="Low"])'))
76
        sum_low += low
77
78
        medium = int(results.xpath('count(//result/threat[text()="Medium"])'))
79
        sum_medium += medium
80
81
        high = int(results.xpath('count(//result/threat[text()="High"])'))
82
        sum_high += high
83
84
        best_os_cpe_report_id = asset.xpath(
85
            'host/detail/name[text()="best_os_cpe"]/../source/@id'
86
        )[0]
87
88
        table_data.append(
89
            [hostname, ip, best_os_cpe_report_id, high, medium, low]
90
        )
91
92
    table = AsciiTable(table_data)
93
    print(table.table + '\n')
94
    print(
95
        'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}'
96
        '\nLow: {2}\n\n'.format(
97
            int(sum_high),
98
            int(sum_medium),
99
            int(sum_low),
100
            from_date.isoformat(),
101
            to_date.isoformat(),
102
        )
103
    )
104
105
106 View Code Duplication
def main(gmp, args):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
    # pylint: disable=undefined-variable
108
109
    check_args(args)
110
111
    month = int(args.script[1])
112
    year = int(args.script[2])
113
    from_date = date(year, month, 1)
114
    to_date = from_date + timedelta(days=31)
115
    # To have the first day in month
116
    to_date = to_date.replace(day=1)
117
118
    print_reports(gmp, from_date, to_date)
119
120
121
if __name__ == '__gmp__':
122
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
123