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 will update target hosts information for a desired task. |
||
30 | Two parameters after the script name are required. |
||
31 | |||
32 | 1. <hosts_file> -- .csv file containing desired target hosts separated by ',' |
||
33 | 2. <task_uuid> -- uuid of task to be modified |
||
34 | |||
35 | Example for starting up the routine: |
||
36 | $ gvm-script --gmp-username name --gmp-password pass \ |
||
37 | ssh --hostname <gsm> scripts/update-task-target.gmp.py hosts_file.csv \ |
||
38 | "303fa0a6-aa9b-43c4-bac0-66ae0b2d1698" |
||
39 | |||
40 | """ |
||
41 | print(message) |
||
42 | sys.exit() |
||
43 | |||
44 | |||
45 | def load_host_file(filename): |
||
46 | host_list = list() |
||
47 | |||
48 | try: |
||
49 | f = open(filename) |
||
50 | for line in f: |
||
51 | host = line.split(",")[0] |
||
52 | host = host.strip() |
||
53 | if len(host) == 0: |
||
54 | continue |
||
55 | host_list.append(host) |
||
56 | |||
57 | except IOError as e: |
||
58 | error_and_exit("Failed to read host_file: {} (exit)".format(str(e))) |
||
59 | |||
60 | if len(host_list) == 0: |
||
61 | error_and_exit("Host file is empty (exit)") |
||
62 | |||
63 | hosts_string = ', '.join(map(str, host_list)) |
||
64 | |||
65 | return hosts_string |
||
66 | |||
67 | |||
68 | def copy_send_target(gmp, hosts_file, old_target_id): |
||
69 | hosts_string = load_host_file(hosts_file) |
||
70 | keywords = {'hosts': hosts_string} |
||
71 | |||
72 | keywords['comment'] = 'This target was automatically modified: {}'.format( |
||
73 | time.strftime("%Y/%m/%d-%H:%M:%S") |
||
74 | ) |
||
75 | |||
76 | old_target = gmp.get_target(target_id=old_target_id)[0] |
||
77 | |||
78 | objects = ('reverse_lookup_only', 'reverse_lookup_unify', 'name') |
||
79 | for obj in objects: |
||
80 | var = old_target.xpath('{}/text()'.format(obj))[0] |
||
81 | if var == '0': |
||
82 | var = '' |
||
83 | keywords['{}'.format(obj)] = var |
||
84 | |||
85 | port_list = {} |
||
86 | port_list = old_target.xpath('port_list/@id')[0] |
||
87 | keywords['port_list_id'] = port_list |
||
88 | |||
89 | keywords['name'] += "_copy" # the name must differ from existing names |
||
90 | |||
91 | new_target_id = gmp.create_target(**keywords).xpath('@id')[0] |
||
92 | |||
93 | print('\n New target created!\n') |
||
94 | print('Target_id: {}'.format(new_target_id)) |
||
95 | print('Target_name: {}\n'.format(keywords['name'])) |
||
96 | |||
97 | return new_target_id |
||
98 | |||
99 | |||
100 | def create_target_hosts(gmp, host_file, task_id, old_target_id): |
||
101 | new_target_id = copy_send_target(gmp, host_file, old_target_id) |
||
102 | |||
103 | gmp.modify_task(task_id=task_id, target_id=new_target_id) |
||
104 | |||
105 | print(' Task successfully modified!\n') |
||
106 | |||
107 | |||
108 | def check_to_delete(gmp, target_id): |
||
109 | target = gmp.get_target(target_id=target_id)[0] |
||
110 | if '0' in target.xpath("in_use/text()"): |
||
111 | gmp.delete_target(target_id=target_id) |
||
112 | |||
113 | |||
114 | def main(gmp, args): |
||
115 | # pylint: disable=undefined-variable |
||
116 | |||
117 | check_args(args) |
||
118 | |||
119 | hosts_file = args.script[1] |
||
120 | task_id = args.script[2] |
||
121 | |||
122 | task = gmp.get_task(task_id=task_id)[1] |
||
123 | old_target_id = task.xpath('target/@id')[0] |
||
124 | |||
125 | create_target_hosts(gmp, hosts_file, task_id, old_target_id) |
||
126 | check_to_delete(gmp, old_target_id) |
||
127 | |||
128 | |||
129 | if __name__ == '__gmp__': |
||
130 | main(gmp, args) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() Comprehensibility
Best Practice
introduced
by
|
|||
131 |