forked from PaloAltoNetworks/SkilletBuilder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration_explorer.py
108 lines (90 loc) · 2.84 KB
/
configuration_explorer.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
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
#!/usr/bin/env python3
#
# Tool to explore a PAN-OS Configuration
#
import json
import os
import xmltodict
from lxml import etree
from lxml.etree import Element
from skilletlib.exceptions import LoginException
from skilletlib.exceptions import SkilletLoaderException
from skilletlib.panoply import Panos
source = os.environ.get('source', 'online')
config = ''
if source == 'online':
config_source = os.environ.get('config_source', 'running')
username = os.environ.get('TARGET_USERNAME', 'admin')
password = os.environ.get('TARGET_PASSWORD', 'admin')
ip = os.environ.get('TARGET_IP', '')
try:
device = Panos(hostname=ip, api_username=username, api_password=password, debug=False)
config = device.get_configuration(config_source=config_source)
except SkilletLoaderException as se:
print('Error Executing Skillet')
print(se)
exit(1)
except LoginException as le:
print('Error Logging into device')
print(le)
exit(1)
else:
config = os.environ.get('config', '')
xpath = os.environ.get('xpath',
"/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/profiles"
)
try:
config_doc = etree.fromstring(config)
found = config_doc.xpath(xpath)
if isinstance(found, list):
if len(found) == 1:
found_item = found.pop(0)
if isinstance(found_item, str):
found_str = str(found_item)
found_obj = found_str
else:
found_str = etree.tostring(found_item).decode('UTF-8')
found_obj = xmltodict.parse(found_str)
else:
found_obj = list()
found_str = 'List of items:\n\n'
for found_item in found:
if isinstance(found_item, str):
found_str = f'{found_str}\n{found_item}'
found_obj.append(found_item)
else:
found_item_str = etree.tostring(found_item).decode('UTF-8')
found_str = f'{found_str}\n{found_item_str}'
found_obj.append(xmltodict.parse(found_item_str))
elif isinstance(found, str):
found_obj = found
found_str = found
else:
found_str = etree.tostring(found)
found_obj = xmltodict.parse(found_str)
found_json = json.dumps(found_obj, indent=' ')
print()
print('=' * 137)
print()
print('Execution Results:')
print()
print('=' * 137)
print()
print(f'xpath: {xpath}')
print()
print('=' * 137)
print()
print('xml:')
print(found_str)
print()
print('=' * 137)
print()
print('json:')
print(found_json)
print()
print('=' * 137)
print()
except ValueError as ve:
print('Could not parse config document!')
print(ve)
exit(1)