|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 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 base64 import b64decode |
|
20
|
|
|
from pathlib import Path |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def check_args(args): |
|
24
|
|
|
len_args = len(args.script) - 1 |
|
25
|
|
|
if len_args < 1: |
|
26
|
|
|
message = """ |
|
27
|
|
|
This script requests the given report and saves it as a pdf file locally. |
|
28
|
|
|
It needs one parameters after the script name. |
|
29
|
|
|
|
|
30
|
|
|
1. <report_id> -- ID of the report |
|
31
|
|
|
|
|
32
|
|
|
Optional a file name to save the pdf in. |
|
33
|
|
|
|
|
34
|
|
|
Example: |
|
35
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
|
36
|
|
|
ssh --hostname <gsm> scripts/pdf-report.gmp.py <report_id> <pdf_file> |
|
37
|
|
|
""" |
|
38
|
|
|
print(message) |
|
39
|
|
|
quit() |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def main(gmp, args): |
|
43
|
|
|
# check if report id and PDF filename are provided to the script |
|
44
|
|
|
# argv[0] contains the script name |
|
45
|
|
|
check_args(args) |
|
46
|
|
|
|
|
47
|
|
|
report_id = args.argv[1] |
|
48
|
|
|
if len(args.argv) == 3: |
|
49
|
|
|
pdf_filename = args.argv[2] |
|
50
|
|
|
else: |
|
51
|
|
|
pdf_filename = args.argv[1] + ".pdf" |
|
52
|
|
|
|
|
53
|
|
|
pdf_report_format_id = "c402cc3e-b531-11e1-9163-406186ea4fc5" |
|
54
|
|
|
|
|
55
|
|
|
response = gmp.get_report( |
|
56
|
|
|
report_id=report_id, report_format_id=pdf_report_format_id |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
report_element = response[0] |
|
60
|
|
|
# get the full content of the report element |
|
61
|
|
|
content = "".join(report_element.itertext()) |
|
62
|
|
|
|
|
63
|
|
|
# convert content to 8-bit ASCII bytes |
|
64
|
|
|
binary_base64_encoded_pdf = content.encode('ascii') |
|
65
|
|
|
|
|
66
|
|
|
# decode base64 |
|
67
|
|
|
binary_pdf = b64decode(binary_base64_encoded_pdf) |
|
68
|
|
|
|
|
69
|
|
|
# write to file and support ~ in filename path |
|
70
|
|
|
pdf_path = Path(pdf_filename).expanduser() |
|
71
|
|
|
|
|
72
|
|
|
pdf_path.write_bytes(binary_pdf) |
|
73
|
|
|
|
|
74
|
|
|
print('Done. PDF created: ' + str(pdf_path)) |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if __name__ == '__gmp__': |
|
78
|
|
|
main(gmp, args) |
|
|
|
|
|
|
79
|
|
|
|