Completed
Push — master ( f07019...0c998d )
by
unknown
18s queued 13s
created

gen-random-targets.gmp   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 9

5 Functions

Rating   Name   Duplication   Size   Complexity  
A n_ip() 0 9 2
A generate() 0 12 3
A check_args() 0 20 2
A rand_number() 0 2 1
A main() 0 11 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-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 as r
20
21
22
def check_args(args):
23
    len_args = len(args.script) - 1
24
    if len_args < 2:
25
        message = """
26
        This script generates random task data and feeds it to\
27
    a desired GSM
28
        It needs two parameters after the script name.
29
30
        1. <host_number> -- number of dummy hosts to select from
31
        2. <number>      -- number of targets to be generated
32
33
        In addition, if you would like for the number of targets generated
34
    to be randomized on a Gaussian distribution, add 'with-gauss'
35
36
        Example:
37
            $ gvm-script --gmp-username name --gmp-password pass \
38
    ssh --hostname <gsm> scripts/gen-random-targets.gmp.py 3 40 with-gauss
39
        """
40
        print(message)
41
        quit()
42
43
44
def rand_number():
45
    return r.randrange(256)
46
47
48
def n_ip(number_of_ips):
49
    list_of_ips = []
50
    for _ in range(number_of_ips):
51
        list_of_ips.append(
52
            '{}.{}.{}.{}'.format(
53
                rand_number(), rand_number(), rand_number(), rand_number()
54
            )
55
        )
56
    return list_of_ips
57
58
59
def generate(gmp, args, n_targets, n_ips):
60
    ips = n_ip(n_ips)
61
62
    if 'with-gauss' in args.script:
63
        n_targets = int(r.gauss(n_targets, 2))
64
65
    for i in range(n_targets):
66
        host_ip = r.choice(ips)
67
        index = '{{0:0>{}}}'.format(len(str(n_targets)))
68
        name = 'Target_{}'.format(index.format(i + 1))
69
70
        gmp.create_target(name=name, make_unique=True, hosts=[host_ip])
71
72
73
def main(gmp, args):
74
    # pylint: disable=undefined-variable
75
76
    check_args(args)
77
78
    host_number = int(args.script[1])
79
    number_targets = int(args.script[2])
80
81
    print('Generating random targets...')
82
83
    generate(gmp, args, number_targets, host_number)
84
85
86
if __name__ == '__gmp__':
87
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
88