1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
# SPDX-FileCopyrightText: 2017-2021 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
from argparse import Namespace
from gvm.errors import GvmResponseError
from gvm.protocols.gmp import Gmp
def clean_sensor(gmp: Gmp) -> None:
tasks = gmp.get_tasks(
filter_string="rows=-1 not status=Running and "
"not status=Requested and not "
"status="Stop Requested""
)
try:
for task_id in tasks.xpath("task/@id"):
print(f"Removing task {task_id} ... ")
status_text = gmp.delete_task(task_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
targets = gmp.get_targets(filter_string="rows=-1 not _owner=""")
try:
for target_id in targets.xpath("target/@id"):
print(f"Removing target {target_id} ... ")
status_text = gmp.delete_target(target_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
configs = gmp.get_scan_configs(
filter_string="rows=-1 not _owner="""
)
try:
for config_id in configs.xpath("config/@id"):
print(f"Removing config {config_id} ... ")
status_text = gmp.delete_scan_config(
config_id, ultimate=True
).xpath("@status_text")[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
port_lists = gmp.get_port_lists(
filter_string="rows=-1 not _owner="""
)
try:
for port_list_id in port_lists.xpath("port_list/@id"):
print(f"Removing port_list {port_list_id} ... ")
status_text = gmp.delete_port_list(
port_list_id, ultimate=True
).xpath("@status_text")[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
alerts = gmp.get_alerts(filter_string="rows=-1 not _owner=""")
try:
for alert_id in alerts.xpath("alert/@id"):
print(f"Removing alert {alert_id} ... ")
status_text = gmp.delete_alert(alert_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
schedules = gmp.get_schedules(
filter_string="rows=-1 not _owner="""
)
try:
for schedule_id in schedules.xpath("schedule/@id"):
print(f"Removing schedule {schedule_id} ... ")
status_text = gmp.delete_schedule(schedule_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
tags = gmp.get_tags(filter_string="rows=-1 not _owner=""")
try:
for tag_id in tags.xpath("tag/@id"):
print(f"Removing tag {tag_id} ... ")
status_text = gmp.delete_tag(tag_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
filters = gmp.get_filters(filter_string="rows=-1 not _owner=""")
try:
for filter_id in filters.xpath("filter/@id"):
print(f"Removing filter {filter_id} ... ")
status_text = gmp.delete_filter(filter_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
credentials = gmp.get_credentials(
filter_string="rows=-1 not _owner="""
)
try:
for config_id in credentials.xpath("credential/@id"):
print(f"Removing credential {config_id} ... ")
status_text = gmp.delete_credential(config_id, ultimate=True).xpath(
"@status_text"
)[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
print("Emptying trash... ")
try:
status_text = gmp.empty_trashcan().xpath("@status_text")[0]
print(status_text)
except GvmResponseError as gvmerr:
print(f"{gvmerr=}")
pass
def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=unused-argument
print(
"This script removes all resources from a sensor, except active tasks.\n"
)
clean_sensor(gmp)
if __name__ == "__gmp__":
main(gmp, args)
|