1 | # -*- coding: utf-8 -*- |
||
2 | # Copyright (C) 2017-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 sys |
||
20 | import time |
||
21 | |||
22 | from gvmtools.helper import error_and_exit |
||
23 | |||
24 | |||
25 | def check_args(args): |
||
26 | len_args = len(args.script) - 1 |
||
27 | if len_args != 2: |
||
28 | message = """ |
||
29 | This script pulls hostnames from a text file and creates a target \ |
||
30 | for each. |
||
31 | One parameter after the script name is required. |
||
32 | |||
33 | 1. <hostname> -- IP of the GVM host |
||
34 | 2. <hosts_textfile> -- text file containing hostnames |
||
35 | |||
36 | Example: |
||
37 | $ gvm-script --gmp-username name --gmp-password pass \ |
||
38 | ssh --hostname <gsm> scripts/create_targets_from_host_list.gmp \ |
||
39 | <hostname> <hosts_textfile> |
||
40 | """ |
||
41 | print(message) |
||
42 | sys.exit() |
||
43 | |||
44 | |||
45 | def load_host_list(host_file): |
||
46 | try: |
||
47 | with open(host_file) as f: |
||
48 | content = f.readlines() |
||
49 | host_list = [x.strip() for x in content] |
||
50 | host_list = list(filter(None, host_list)) |
||
51 | except IOError as e: |
||
52 | error_and_exit("Failed to read host_file: {} (exit)".format(str(e))) |
||
53 | |||
54 | if len(host_list) == 0: |
||
55 | error_and_exit("Host file is empty (exit)") |
||
56 | |||
57 | return host_list |
||
58 | |||
59 | |||
60 | def send_targets(gmp, host_name, host_file, host_list): |
||
61 | print('\nSending targets from {} to {}...'.format(host_file, host_name)) |
||
62 | |||
63 | for host in host_list: |
||
64 | name = "Target for {}".format(host) |
||
65 | comment = "Created: {}".format(time.strftime("%Y/%m/%d-%H:%M:%S")) |
||
66 | hosts = [host] |
||
67 | |||
68 | gmp.create_target(name=name, comment=comment, hosts=hosts) |
||
69 | |||
70 | |||
71 | def main(gmp, args): |
||
72 | # pylint: disable=undefined-variable |
||
73 | |||
74 | check_args(args) |
||
75 | |||
76 | hostname = args.script[1] |
||
77 | hostfile = args.script[2] |
||
78 | |||
79 | hostlist = load_host_list(hostfile) |
||
80 | send_targets(gmp, hostname, hostfile, hostlist) |
||
81 | |||
82 | print('\n Target(s) created!\n') |
||
83 | |||
84 | |||
85 | if __name__ == '__gmp__': |
||
86 | main(gmp, args) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() Comprehensibility
Best Practice
introduced
by
|
|||
87 |