1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 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
|
|
|
from datetime import date, timedelta |
20
|
|
|
|
21
|
|
|
from argparse import ArgumentParser, RawTextHelpFormatter |
22
|
|
|
|
23
|
|
|
HELP_TEXT = "" |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def get_last_reports_from_tasks(gmp, from_date, to_date): |
27
|
|
|
task_filter = "rows=-1 and created>{0} and created<{1}".format( |
28
|
|
|
from_date.isoformat(), to_date.isoformat() |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
tasks_xml = gmp.get_tasks(filter=task_filter) |
32
|
|
|
for report in tasks_xml.xpath('task/last_report/report/@id'): |
33
|
|
|
print(report) |
34
|
|
|
|
35
|
|
|
|
36
|
|
View Code Duplication |
def combine_reports(gmp, args): |
|
|
|
|
37
|
|
|
new_uuid = generate_uuid() |
|
|
|
|
38
|
|
|
combined_report = e.Element( |
|
|
|
|
39
|
|
|
'report', |
40
|
|
|
{ |
41
|
|
|
'id': new_uuid, |
42
|
|
|
'format_id': 'd5da9f67-8551-4e51-807b-b6a873d70e34', |
43
|
|
|
'extension': 'xml', |
44
|
|
|
'content_type': 'text/xml', |
45
|
|
|
}, |
46
|
|
|
) |
47
|
|
|
report_elem = e.Element('report', {'id': new_uuid}) |
48
|
|
|
ports_elem = e.Element('ports', {'start': '1', 'max': '-1'}) |
49
|
|
|
results_elem = e.Element('results', {'start': '1', 'max': '-1'}) |
50
|
|
|
combined_report.append(report_elem) |
51
|
|
|
report_elem.append(results_elem) |
52
|
|
|
|
53
|
|
|
if 'first_task' in args.script: |
54
|
|
|
arg_len = args.script[1:-1] |
55
|
|
|
else: |
56
|
|
|
arg_len = args.script[1:] |
57
|
|
|
|
58
|
|
|
hosts = [] |
59
|
|
|
for argument in arg_len: |
60
|
|
|
current_report = gmp.get_report(argument, details=True)[0] |
61
|
|
|
for port in current_report.xpath('report/ports/port'): |
62
|
|
|
ports_elem.append(port) |
63
|
|
|
for result in current_report.xpath('report/results/result'): |
64
|
|
|
results_elem.append(result) |
65
|
|
|
for host in current_report.xpath('report/host'): |
66
|
|
|
report_elem.append(host) |
67
|
|
|
|
68
|
|
|
return combined_report |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def parse_args(args): # pylint: disable=unused-argument |
72
|
|
|
parser = ArgumentParser( |
73
|
|
|
prefix_chars="+", |
74
|
|
|
add_help=False, |
75
|
|
|
formatter_class=RawTextHelpFormatter, |
76
|
|
|
description=HELP_TEXT, |
77
|
|
|
) |
78
|
|
|
|
79
|
|
|
parser.add_argument( |
80
|
|
|
"+h", |
81
|
|
|
"++help", |
82
|
|
|
action="help", |
83
|
|
|
help="Show this help message and exit.", |
84
|
|
|
) |
85
|
|
|
|
86
|
|
|
parser.add_argument( |
87
|
|
|
"+d", |
88
|
|
|
"++date", |
89
|
|
|
type=str, |
90
|
|
|
required=True, |
91
|
|
|
dest="date", |
92
|
|
|
help="The month and year to collect reports from: (mm/yyyy)", |
93
|
|
|
) |
94
|
|
|
|
95
|
|
|
parser.add_argument( |
96
|
|
|
"+t", |
97
|
|
|
"++tags", |
98
|
|
|
nargs='+', |
99
|
|
|
type=str, |
100
|
|
|
dest="tags", |
101
|
|
|
help="Filter the reports by given tag(s).", |
102
|
|
|
) |
103
|
|
|
|
104
|
|
|
script_args, _ = parser.parse_known_args() |
105
|
|
|
return script_args |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def main(gmp, args): |
109
|
|
|
# pylint: disable=undefined-variable |
110
|
|
|
|
111
|
|
|
parsed_args = parse_args(args) |
112
|
|
|
|
113
|
|
|
month, year = parsed_args.date.split('/') |
114
|
|
|
from_date = date(int(year), int(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(from_date) |
120
|
|
|
print(to_date) |
121
|
|
|
|
122
|
|
|
get_last_reports_from_tasks(gmp, from_date, to_date) |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
if __name__ == '__gmp__': |
126
|
|
|
main(gmp, args) |
|
|
|
|
127
|
|
|
|