|
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
|
|
|
from gvmtools.helper import create_xml_tree |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def check_args(args): |
|
24
|
|
|
len_args = len(args.script) - 1 |
|
25
|
|
|
if len_args != 1: |
|
26
|
|
|
message = """ |
|
27
|
|
|
This script pulls schedule data from an xml document and feeds it to \ |
|
28
|
|
|
a desired GSM |
|
29
|
|
|
One parameter after the script name is required. |
|
30
|
|
|
|
|
31
|
|
|
1. <xml_doc> -- .xml file containing schedules |
|
32
|
|
|
|
|
33
|
|
|
Example: |
|
34
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
|
35
|
|
|
ssh --hostname <gsm> scripts/send-schedules.gmp.py example_file.xml |
|
36
|
|
|
|
|
37
|
|
|
""" |
|
38
|
|
|
print(message) |
|
39
|
|
|
sys.exit() |
|
40
|
|
|
if int(gmp.get_protocol_version()[0]) < 8: |
|
|
|
|
|
|
41
|
|
|
print("This script requires GMP version 8") |
|
42
|
|
|
sys.exit() |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def parse_send_xml_tree(gmp, xml_tree): |
|
46
|
|
|
for schedule in xml_tree.xpath('schedule'): |
|
47
|
|
|
name = schedule.find('name').text |
|
48
|
|
|
|
|
49
|
|
|
comment = schedule.find('comment').text |
|
50
|
|
|
if comment is None: |
|
51
|
|
|
comment = '' |
|
52
|
|
|
|
|
53
|
|
|
ical = schedule.find('icalendar').text |
|
54
|
|
|
|
|
55
|
|
|
timezone = schedule.find('timezone').text |
|
56
|
|
|
|
|
57
|
|
|
gmp.create_schedule( |
|
58
|
|
|
name=name, comment=comment, timezone=timezone, icalendar=ical |
|
59
|
|
|
) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def main(gmp, args): |
|
63
|
|
|
# pylint: disable=undefined-variable |
|
64
|
|
|
|
|
65
|
|
|
check_args(args) |
|
66
|
|
|
|
|
67
|
|
|
xml_doc = args.script[1] |
|
68
|
|
|
|
|
69
|
|
|
print('\nSending schedules...') |
|
70
|
|
|
|
|
71
|
|
|
xml_tree = create_xml_tree(xml_doc) |
|
72
|
|
|
parse_send_xml_tree(gmp, xml_tree) |
|
73
|
|
|
|
|
74
|
|
|
print('\n Schedule(s) created!\n') |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if __name__ == '__gmp__': |
|
78
|
|
|
main(gmp, args) |
|
|
|
|
|
|
79
|
|
|
|