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

scan-new-system.gmp.create_target()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
20
def check_args(args):
21
    len_args = len(args.script) - 1
22
    message = """
23
        This script starts a new scan on the given host.
24
        It needs one parameters after the script name.
25
26
        1. <host_ip>     -- IP Address of the host system
27
        
28
                Example:
29
            $ gvm-script --gmp-username name --gmp-password pass \
30
ssh --hostname <gsm> scripts/scan-new-system.gmp.py <host_ip>
31
    """
32
    if len_args != 1:
33
        print(message)
34
        quit()
35
36
37
def create_target(gmp, ipaddress):
38
    import datetime
39
40
    # create a unique name by adding the current datetime
41
    name = "Suspect Host {} {}".format(ipaddress, str(datetime.datetime.now()))
42
    response = gmp.create_target(name=name, hosts=[ipaddress])
43
    return response.get('id')
44
45
46
def create_task(gmp, ipaddress, target_id, scan_config_id, scanner_id):
47
    name = "Scan Suspect Host {}".format(ipaddress)
48
    response = gmp.create_task(
49
        name=name,
50
        config_id=scan_config_id,
51
        target_id=target_id,
52
        scanner_id=scanner_id,
53
    )
54
    return response.get('id')
55
56
57
def start_task(gmp, task_id):
58
    response = gmp.start_task(task_id)
59
    # the response is
60
    # <start_task_response><report_id>id</report_id></start_task_response>
61
    return response[0].text
62
63
64
def main(gmp, args):
65
    # pylint: disable=undefined-variable
66
67
    check_args(args)
68
69
    ipaddress = args.argv[1]
70
71
    target_id = create_target(gmp, ipaddress)
72
73
    full_and_fast_scan_config_id = 'daba56c8-73ec-11df-a475-002264764cea'
74
    openvas_scanner_id = '08b69003-5fc2-4037-a479-93b440211c73'
75
    task_id = create_task(
76
        gmp,
77
        ipaddress,
78
        target_id,
79
        full_and_fast_scan_config_id,
80
        openvas_scanner_id,
81
    )
82
83
    report_id = start_task(gmp, task_id)
84
85
    print(
86
        "Started scan of host {}. Corresponding report ID is {}".format(
87
            ipaddress, report_id
88
        )
89
    )
90
91
92
if __name__ == '__gmp__':
93
    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...
94