-
Notifications
You must be signed in to change notification settings - Fork 90
/
bulk-modify-schedules.gmp.py
75 lines (54 loc) · 1.98 KB
/
bulk-modify-schedules.gmp.py
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
# SPDX-FileCopyrightText: 2021 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
import sys
from argparse import Namespace
from gvm.protocols.gmp import Gmp
def check_args(args):
len_args = len(args.script) - 1
if len_args != 3:
message = """
This script modifies the timezone and/or icalendar of all schedules
in a filter selection.
<filter> -- the filter text used to filter the schedules.
<timezone> -- the new timezone to set or empty to keep existing one.
<icalendar> -- the new icalendar to set or empty to keep existing one.
Example:
$ gvm-script --gmp-username name --gmp-password pass \\
ssh --hostname <gsm> scripts/bulk-modify-schedules.gmp.py \\
<filter> <timezone> <icalendar>
"""
print(message)
sys.exit()
def bulk_modify_schedules(gmp, filter_term, new_timezone, new_icalendar):
get_response = gmp.get_schedules(filter=filter_term)
schedules = get_response.findall("schedule")
for schedule in schedules:
uuid = schedule.attrib["id"]
name = schedule.find("name").text
comment = schedule.find("comment").text
if new_timezone:
timezone = new_timezone
else:
timezone = schedule.find("timezone").text
if new_icalendar:
icalendar = new_icalendar
else:
icalendar = schedule.find("icalendar").text
print(f"- Modifying {name} ({uuid})")
gmp.modify_schedule(
uuid,
name=name,
comment=comment,
timezone=timezone,
icalendar=icalendar,
)
def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=unused-argument
check_args(args)
filter_term = args.script[1]
new_timezone = args.script[2]
new_icalendar = args.script[3]
bulk_modify_schedules(gmp, filter_term, new_timezone, new_icalendar)
if __name__ == "__gmp__":
main(gmp, args)