Issues (69)

scripts/scan-new-system.gmp.py (2 issues)

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019-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 datetime
20
import sys
21
22
23
def check_args(args):
24
    len_args = len(args.script) - 1
25
    message = """
26
        This script starts a new scan on the given host.
27
        It needs one parameters after the script name.
28
29
        1. <host_ip>     -- IP Address of the host system
30
        2. <port_list_id>     -- Port List UUID for scanning the host system. Preconfigured UUID might be under /var/lib/gvm/data-objects/gvmd/20.08/port_lists/. ex. iana-tcp-udp is "4a4717fe-57d2-11e1-9a26-406186ea4fc5".
31
        
32
                Example:
33
            $ gvm-script --gmp-username name --gmp-password pass \
34
ssh --hostname <gsm> scripts/scan-new-system.gmp.py <host_ip> <port_list_id>
35
    """
36
    if len_args != 2:
37
        print(message)
38
        sys.exit()
39
40
41
def create_target(gmp, ipaddress, port_list_id):
42
    # create a unique name by adding the current datetime
43
    name = "Suspect Host {} {}".format(ipaddress, str(datetime.datetime.now()))
44
45
    response = gmp.create_target(
46
        name=name, hosts=[ipaddress], port_list_id=port_list_id
47
    )
48
    return response.get('id')
49
50
51
def create_task(gmp, ipaddress, target_id, scan_config_id, scanner_id):
52
    name = "Scan Suspect Host {}".format(ipaddress)
53
    response = gmp.create_task(
54
        name=name,
55
        config_id=scan_config_id,
56
        target_id=target_id,
57
        scanner_id=scanner_id,
58
    )
59
    return response.get('id')
60
61
62
def start_task(gmp, task_id):
63
    response = gmp.start_task(task_id)
64
    # the response is
65
    # <start_task_response><report_id>id</report_id></start_task_response>
66
    return response[0].text
67
68
69
def main(gmp, args):
70
71
    check_args(args)
72
73
    ipaddress = args.argv[1]
74
    port_list_id = args.argv[2]
75
76
    target_id = create_target(gmp, ipaddress, port_list_id)
77
78
    full_and_fast_scan_config_id = 'daba56c8-73ec-11df-a475-002264764cea'
79
    openvas_scanner_id = '08b69003-5fc2-4037-a479-93b440211c73'
80
    task_id = create_task(
81
        gmp,
82
        ipaddress,
83
        target_id,
84
        full_and_fast_scan_config_id,
85
        openvas_scanner_id,
86
    )
87
88
    report_id = start_task(gmp, task_id)
89
90
    print(
91
        "Started scan of host {}. Corresponding report ID is {}".format(
92
            ipaddress, report_id
93
        )
94
    )
95
96
97
if __name__ == '__gmp__':
98
    # pylint: disable=undefined-variable
99
    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...
100