|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2018-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
|
|
|
import sys |
|
20
|
|
|
|
|
21
|
|
|
from lxml import etree as e |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def check_args(args): |
|
25
|
|
|
len_args = len(args.script) - 1 |
|
26
|
|
|
if len_args is not 1: |
|
27
|
|
|
message = """ |
|
28
|
|
|
This script pulls schedule data from an xml document and feeds it to \ |
|
29
|
|
|
a desired GSM |
|
30
|
|
|
One parameter after the script name is required. |
|
31
|
|
|
|
|
32
|
|
|
1. <xml_doc> -- .xml file containing schedules |
|
33
|
|
|
|
|
34
|
|
|
Example: |
|
35
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
|
36
|
|
|
ssh --hostname <gsm> scripts/send-schedules.gmp.py targethost example_file.xml |
|
37
|
|
|
|
|
38
|
|
|
""" |
|
39
|
|
|
print(message) |
|
40
|
|
|
quit() |
|
41
|
|
|
if int(gmp.get_protocol_version()[0]) < 8: |
|
|
|
|
|
|
42
|
|
|
print("This script requires GMP version 8") |
|
43
|
|
|
quit() |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def error_and_exit(msg): |
|
47
|
|
|
print("Error: {}\n".format(msg), file=sys.stderr) |
|
48
|
|
|
sys.exit(1) |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
def create_xml_tree(xml_doc): |
|
|
|
|
|
|
52
|
|
|
try: |
|
53
|
|
|
xml_tree = e.parse(xml_doc) |
|
54
|
|
|
xml_tree = e.tostring(xml_tree) |
|
55
|
|
|
xml_tree = e.XML(xml_tree) |
|
56
|
|
|
except IOError as err: |
|
57
|
|
|
error_and_exit("Failed to read xml_file: {} (exit)".format(str(err))) |
|
58
|
|
|
|
|
59
|
|
|
if len(xml_tree) == 0: |
|
60
|
|
|
error_and_exit("XML file is empty (exit)") |
|
61
|
|
|
|
|
62
|
|
|
return xml_tree |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def parse_send_xml_tree(gmp, xml_tree): |
|
66
|
|
|
for schedule in xml_tree.xpath('schedule'): |
|
67
|
|
|
name = schedule.find('name').text |
|
68
|
|
|
|
|
69
|
|
|
comment = schedule.find('comment').text |
|
70
|
|
|
if comment is None: |
|
71
|
|
|
comment = '' |
|
72
|
|
|
|
|
73
|
|
|
ical = schedule.find('icalendar').text |
|
74
|
|
|
|
|
75
|
|
|
timezone_abbrev = schedule.find('timezone_abbrev').text |
|
76
|
|
|
|
|
77
|
|
|
gmp.create_schedule( |
|
78
|
|
|
name=name, comment=comment, timezone=timezone_abbrev, icalendar=ical |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def main(gmp, args): |
|
83
|
|
|
# pylint: disable=undefined-variable |
|
84
|
|
|
|
|
85
|
|
|
check_args(args) |
|
86
|
|
|
|
|
87
|
|
|
xml_doc = args.script[1] |
|
88
|
|
|
|
|
89
|
|
|
print('\nSending schedules...') |
|
90
|
|
|
|
|
91
|
|
|
xml_tree = create_xml_tree(xml_doc) |
|
92
|
|
|
parse_send_xml_tree(gmp, xml_tree) |
|
93
|
|
|
|
|
94
|
|
|
print('\n Schedule(s) created!\n') |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
if __name__ == '__gmp__': |
|
98
|
|
|
main(gmp, args) |
|
|
|
|
|
|
99
|
|
|
|