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

create-dummy-data.gmp   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
B create_data() 0 40 6
A check_args() 0 14 2
A id_generator() 0 2 1
A main() 0 6 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2017-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
import random
20
import string
21
22
23
def check_args(args):
24
    len_args = len(args.script) - 1
25
    if len_args is not 1:
26
        message = """
27
        This script will create random data in the given GVM database
28
29
        1. <count>  -- Number of datasets to create
30
31
        Example:
32
            $ gvm-script --gmp-username name --gmp-password pass \
33
    ssh --hostname <gsm> scripts/create-dummy-data.gmp.py <count>
34
        """
35
        print(message)
36
        quit()
37
38
39
def id_generator(size=12, chars=string.ascii_uppercase + string.digits):
40
    return ''.join(random.choice(chars) for _ in range(size))
41
42
43
def create_data(gmp, count):
44
    config_ids = []
45
    target_ids = []
46
47
    for _ in range(0, count):
48
        name = id_generator()
49
        gmp.create_credential(
50
            name,
51
            login=name,
52
            password=name,
53
            credential_type=gmp.types.CredentialType.PASSWORD_ONLY,
54
        )
55
    print(str(count) + ' random credentials generated.')
56
57
    for _ in range(0, count):
58
        name = id_generator()
59
        gmp.create_port_list(name, port_range='T:1-42')
60
    print(str(count) + ' random port lists generated.')
61
62
    for _ in range(0, count):
63
        name = id_generator()
64
        res = gmp.create_config('085569ce-73ed-11df-83c3-002264764cea', name)
65
        config_ids.append(res.xpath('@id')[0])
66
    print(str(count) + ' random scan configs generated.')
67
68
    for _ in range(0, count):
69
        name = id_generator()
70
        res = gmp.create_target(name, hosts=['127.0.0.1'])
71
72
        target_ids.append(res.xpath('@id')[0])
73
    print(str(count) + ' random targets generated.')
74
75
    for _ in range(0, count):
76
        name = id_generator()
77
        config_id = random.choice(config_ids)
78
        target_id = random.choice(target_ids)
79
        gmp.create_task(
80
            name, config_id, target_id, '08b69003-5fc2-4037-a479-93b440211c73'
81
        )
82
    print(str(count) + ' random tasks generated.')
83
84
85
def main(gmp, args):
86
    # pylint: disable=undefined-variable
87
88
    check_args(args)
89
90
    create_data(gmp, int(args.script[1]))
91
92
93
if __name__ == '__gmp__':
94
    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...
95