|
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
|
|
|
|
|
20
|
|
|
def clean_sensor(gmp): |
|
21
|
|
|
tasks = gmp.get_tasks( |
|
22
|
|
|
filter="rows=-1 not status=Running and " |
|
23
|
|
|
"not status=Requested and not " |
|
24
|
|
|
"status="Stop Requested"" |
|
25
|
|
|
) |
|
26
|
|
|
|
|
27
|
|
|
for tid in tasks.xpath('task/@id'): |
|
28
|
|
|
print('Removing task %s ... ' % tid) |
|
29
|
|
|
status_text = gmp.delete_task(tid, ultimate=True).xpath('@status_text')[ |
|
30
|
|
|
0 |
|
31
|
|
|
] |
|
32
|
|
|
print(status_text) |
|
33
|
|
|
|
|
34
|
|
|
targets = gmp.get_targets(filter="rows=-1 not _owner=""") |
|
35
|
|
|
for tid in targets.xpath('target/@id'): |
|
36
|
|
|
print('Removing target %s ... ' % tid) |
|
37
|
|
|
status_text = gmp.delete_target(tid, ultimate=True).xpath( |
|
38
|
|
|
'@status_text' |
|
39
|
|
|
)[0] |
|
40
|
|
|
print(status_text) |
|
41
|
|
|
|
|
42
|
|
|
configs = gmp.get_configs(filter="rows=-1 not _owner=""") |
|
43
|
|
|
for cid in configs.xpath('config/@id'): |
|
44
|
|
|
print('Removing config %s ... ' % cid) |
|
45
|
|
|
status_text = gmp.delete_config(cid, ultimate=True).xpath( |
|
46
|
|
|
'@status_text' |
|
47
|
|
|
)[0] |
|
48
|
|
|
print(status_text) |
|
49
|
|
|
|
|
50
|
|
|
port_lists = gmp.get_port_lists(filter="rows=-1 not _owner=""") |
|
51
|
|
|
for pid in port_lists.xpath('port_list/@id'): |
|
52
|
|
|
print('Removing port_list %s ... ' % pid) |
|
53
|
|
|
status_text = gmp.delete_port_list(pid, ultimate=True).xpath( |
|
54
|
|
|
'@status_text' |
|
55
|
|
|
)[0] |
|
56
|
|
|
print(status_text) |
|
57
|
|
|
|
|
58
|
|
|
credentials = gmp.get_credentials(filter="rows=-1 not _owner=""") |
|
59
|
|
|
for cid in credentials.xpath('credential/@id'): |
|
60
|
|
|
print('Removing credential %s ... ' % cid) |
|
61
|
|
|
status_text = gmp.delete_credential(cid, ultimate=True).xpath( |
|
62
|
|
|
'@status_text' |
|
63
|
|
|
)[0] |
|
64
|
|
|
print(status_text) |
|
65
|
|
|
|
|
66
|
|
|
print('Emptying trash... ') |
|
67
|
|
|
status_text = gmp.empty_trashcan().xpath('@status_text')[0] |
|
68
|
|
|
print(status_text) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
def main(gmp, args): |
|
72
|
|
|
# pylint: disable=undefined-variable |
|
73
|
|
|
|
|
74
|
|
|
message = """ |
|
75
|
|
|
This script removes all resources from a sensor, except active tasks. |
|
76
|
|
|
|
|
77
|
|
|
""" |
|
78
|
|
|
print(message) |
|
79
|
|
|
|
|
80
|
|
|
clean_sensor(gmp) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
if __name__ == '__gmp__': |
|
84
|
|
|
main(gmp, args) |
|
|
|
|
|
|
85
|
|
|
|