File: export-csv-report.gmp.py

package info (click to toggle)
gvm-tools 25.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 10,611; xml: 445; makefile: 27
file content (84 lines) | stat: -rwxr-xr-x 2,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# SPDX-FileCopyrightText: 2024 Martin Boller
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Based on the export-pdf-report script and modified to
# create csv and return more (all) details.

import sys
from argparse import Namespace
from base64 import b64decode
from pathlib import Path

from gvm.protocols.gmp import Gmp


def check_args(args):
    len_args = len(args.script) - 1
    if len_args < 1:
        message = """
        This script requests the given report and exports it as a csv
        file locally. It requires one parameter after the script name.

        1. <report_id>     -- ID of the report

        Optional a file name to save the csv in.

        Examples:
            $ gvm-script --gmp-username name --gmp-password pass \
ssh --hostname <gsm> scripts/export-csv-report.gmp.py <report_id> <csv_file>
            $ gvm-script --gmp-username admin --gmp-password '0f6fa69b-32bb-453a-9aa4-b8c9e56b3d00' socket export-csv-report.gmp.py b26229cd-94c8-44f8-9cb6-27486a3dedad ./test.csv
        """
        print(message)
        sys.exit()


def main(gmp: Gmp, args: Namespace) -> None:
    # check if report id and CSV filename are provided to the script
    # argv[0] contains the script name
    check_args(args)

    report_id = args.argv[1]
    if len(args.argv) == 3:
        csv_filename = args.argv[2] + ".csv"
    else:
        csv_filename = args.argv[1] + ".csv"

    csv_report_format_id = "c1645568-627a-11e3-a660-406186ea4fc5"

    response = gmp.get_report(
        report_id=report_id,
        report_format_id=csv_report_format_id,
        ignore_pagination=True,
        details=True,
    )

    report_element = response.find("report")
    # get the full content of the report element
    content = report_element.find("report_format").tail

    if not content:
        print(
            "Requested report is empty. Either the report does not contain any "
            " results or the necessary tools for creating the report are "
            "not installed.",
            file=sys.stderr,
        )
        sys.exit(1)

    # convert content to 8-bit ASCII bytes
    binary_base64_encoded_csv = content.encode("ascii")

    # decode base64
    binary_csv = b64decode(binary_base64_encoded_csv)

    # write to file and support ~ in filename path
    csv_path = Path(csv_filename).expanduser()

    csv_path.write_bytes(binary_csv)

    print("Done. CSV created: " + str(csv_path))


if __name__ == "__gmp__":
    main(gmp, args)