-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathget_eventlist.py
executable file
·313 lines (274 loc) · 11.8 KB
/
get_eventlist.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
"""
Copyright (C) 2021, Axis Communications AB, Lund, Sweden
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--------------------------------------------------------------------------------
Get declared and sent event lists from an Axis device
Requirements:
Shell: xmllint
sudo apt install libxml2-utils
Python: requests
pip3 install requests
"""
import argparse
import subprocess as subp
import sys
import time
import xml.etree.ElementTree as ET
import requests
from requests.auth import HTTPDigestAuth, HTTPBasicAuth
# Definitions
ONVIFLIST = "onviflist.xml"
SENTLIST = "sentonviflist.xml"
TIMEOUT = 30
APITYPE = "onvif"
# Wrapped commands
XMLLINT_CMD = "xmllint --format -"
# ONVIF - GetEventPropertiesRequest
ONVIF_XML_HEADERS = {'content-type': 'application/xml'}
ONVIF_GEPR_PAYLOAD = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:tet="http://www.onvif.org/ver10/events/wsdl">
<SOAP-ENV:Header>
<wsa:Action>
http://www.onvif.org/ver10/events/wsdl/EventPortType/GetEventPropertiesRequest
</wsa:Action>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<tet:GetEventProperties>
</tet:GetEventProperties>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
# ONVIF - CreatePullPointSubscriptionRequest
#
# It is possible to update from //. to
# e.g. tns1:Monitoring/ProcessorUsage
ONVIF_CPPSR_PAYLOAD = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:tet="http://www.onvif.org/ver10/events/wsdl"
xmlns:tns1="http://www.onvif.org/ver10/topics"
xmlns:wsnt="http://docs.oasis-open.org/wsn/b-2">
<SOAP-ENV:Header>
<wsa:Action>
http://www.onvif.org/ver10/events/wsdl/EventPortType/CreatePullPointSubscriptionRequest
</wsa:Action>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<tet:CreatePullPointSubscription>
<tet:Filter>
<wsnt:TopicExpression Dialect="http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet">//.</wsnt:TopicExpression>
</tet:Filter>
</tet:CreatePullPointSubscription>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
# ONVIF - PullMessagesRequest
ONVIF_PMR_PAYLOAD = """<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:tet="http://www.onvif.org/ver10/events/wsdl">
<SOAP-ENV:Header>
<wsa:Action>
http://www.onvif.org/ver10/events/wsdl/PullPointSubscription/PullMessagesRequest
</wsa:Action>
<wsa:To>http://192.168.0.90/onvif/services</wsa:To>
<dom0:SubscriptionId xmlns:dom0="http://www.axis.com/2009/event">{}</dom0:SubscriptionId>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<tet:PullMessages>
<tet:Timeout>PT5S</tet:Timeout>
<tet:MessageLimit>1000</tet:MessageLimit>
</tet:PullMessages>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
def get_event_list(args):
""" Get list of available events to subscribe to from axevent API
Store event subscribe list to file
"""
# Set API specific options
payload = ONVIF_GEPR_PAYLOAD
xmlfile = ONVIFLIST
print("\n### Calling ONVIF Event Properties Request ###")
print("N.B. This option requires an ONVIF user to be registered on\n" +
"device and those user credentials passed to this call\n")
# Set proxy options
proxies = {'http': args.httpproxy,
'https': args.httpsproxy}
if args.httpproxy:
http = "http"
print("HTTP proxy: {}".format(str(args.httpproxy)))
elif args.httpsproxy:
http = "https"
print("HTTPS proxy: {}".format(str(args.httpsproxy)))
else:
http = "http"
url = "{}://{}/{}/services".format(http, args.ip, APITYPE)
print("Connect to device with URL: {}".format(url))
# Set authentication options
if args.auth == "basic":
auth = HTTPBasicAuth(args.user, args.password)
print("Authentication method: {}".format("basic"))
else:
auth = HTTPDigestAuth(args.user, args.password)
print("Authentication method: {}".format("digest"))
# Make request to device
response = requests.post(url, headers=ONVIF_XML_HEADERS, auth=auth,
data=payload, timeout=TIMEOUT, proxies=proxies)
response.raise_for_status()
# Pretty print with xmllint
if response.status_code == 200:
response.encoding = 'UTF-8'
py_ver = sys.version_info
if py_ver[1] >= 5:
# Python versions >= 3.5 - standard
with open(xmlfile, 'w') as xmlf:
res = subp.run("echo '{}'".format(response.text) + "|" +
XMLLINT_CMD, stdout=xmlf, shell=True, check=True)
if res.returncode:
with open(xmlfile, 'w') as xmlf:
xmlf.write(response.text)
raise Exception("XML Pretty print with xmllint to file failed")
else:
# Python versions < 3.5
with open(xmlfile, 'w') as xmlf:
res = subp.call("echo '{}'".format(response.text) + "|" +
XMLLINT_CMD, stdout=xmlf, shell=True)
if res:
with open(xmlfile, 'w') as xmlf:
xmlf.write(response.text)
raise Exception("XML Pretty print with xmllint to file failed")
def get_sent_list(args):
""" Get list of sent events from ONVIF API
Store sent event list to file
"""
# Set API specific options
payload = ONVIF_CPPSR_PAYLOAD
xmlfile = SENTLIST
print("\n### Calling ONVIF Create Pull Point Subscription Request and\n" +
"ONVIF Pull Messages Request###")
print("N.B. This option requires an ONVIF user to be registered on\n" +
"device and those user credentials passed to this call\n")
# Set proxy options
proxies = {'http': args.httpproxy,
'https': args.httpsproxy}
if args.httpproxy:
http = "http"
print("HTTP proxy: {}".format(str(args.httpproxy)))
elif args.httpsproxy:
http = "https"
print("HTTPS proxy: {}".format(str(args.httpsproxy)))
else:
http = "http"
url = "{}://{}/{}/services".format(http, args.ip, APITYPE)
print("Connect to device with URL: {}".format(url))
# Set authentication options
if args.auth == "basic":
auth = HTTPBasicAuth(args.user, args.password)
print("Authentication method: {}".format("basic"))
else:
auth = HTTPDigestAuth(args.user, args.password)
print("Authentication method: {}".format("digest"))
# Make CreatePullPointSubscription request to device
response = requests.post(url, headers=ONVIF_XML_HEADERS, auth=auth,
data=payload, timeout=TIMEOUT, proxies=proxies)
response.raise_for_status()
if response.status_code == 200:
root = ET.fromstring(response.content)
for child in root.iter('*'):
if "SubscriptionId" in str(child.tag):
subscription_id = child.text
if subscription_id:
print("Subscription id: {}".format(subscription_id))
else:
raise Exception("SubscriptionId is not found in response")
# Wait before PullMessages request to get more events
print("Waiting for {} seconds...".format(args.time))
time.sleep(int(args.time))
print("Waiting finished")
# Make PullMessages request to device
payload = ONVIF_PMR_PAYLOAD.format(subscription_id)
response = requests.post(url, headers=ONVIF_XML_HEADERS, auth=auth,
data=payload, timeout=TIMEOUT, proxies=proxies)
response.raise_for_status()
# Pretty print with xmllint
if response.status_code == 200:
response.encoding = 'UTF-8'
py_ver = sys.version_info
if py_ver[1] >= 5:
# Python versions >= 3.5 - standard
with open(xmlfile, 'w') as xmlf:
res = subp.run("echo '{}'".format(response.text) + "|" +
XMLLINT_CMD, stdout=xmlf, shell=True, check=True)
if res.returncode:
with open(xmlfile, 'w') as xmlf:
xmlf.write(response.text)
raise Exception("XML Pretty print with xmllint to file failed")
else:
# Python versions < 3.5
with open(xmlfile, 'w') as xmlf:
res = subp.call("echo '{}'".format(response.text) + "|" +
XMLLINT_CMD, stdout=xmlf, shell=True)
if res:
with open(xmlfile, 'w') as xmlf:
xmlf.write(response.text)
raise Exception("XML Pretty print with xmllint to file failed")
def main():
""" Parse arguments and call correct method """
# Parse arguments - top level
description = "Get list of events from an Axis device.\n"
parser = argparse.ArgumentParser(description=description)
subparsers = parser.add_subparsers()
# Get list arguments
get_list = subparsers.add_parser("getlist", help='Get list of declared ' +
'events from ONVIF API')
get_list.add_argument("-a", "--auth", default="digest",
help="Authentication method: basic, digest (def: digest)")
get_list.add_argument("-u", "--user", default="root",
help="ONVIF User name on device (def: root)")
get_list.add_argument("-p", "--password", default="pass",
help="Password for ONVIF user on device (def: pass)")
get_list.add_argument("-i", "--ip", default="192.168.0.90",
help="IP-address to Axis device (def: 192.168.0.90)")
get_list.add_argument("--httpproxy", default=None,
help="Optional http proxy (def: None)")
get_list.add_argument("--httpsproxy", default=None,
help="Optional https proxy (def: None)")
get_list.set_defaults(func=get_event_list)
# Get sent arguments
get_list = subparsers.add_parser("getsent", help='Get list of sent ' +
'events from ONVIF API')
get_list.add_argument("-a", "--auth", default="digest",
help="Authentication method: basic, digest (def: digest)")
get_list.add_argument("-u", "--user", default="root",
help="ONVIF User name on device (def: root)")
get_list.add_argument("-p", "--password", default="pass",
help="Password for ONVIF user on device (def: pass)")
get_list.add_argument("-i", "--ip", default="192.168.0.90",
help="IP-address to Axis device (def: 192.168.0.90)")
get_list.add_argument("-t", "--time", default="10",
help="Time, in seconds, for fetching events (def: 10)")
get_list.add_argument("--httpproxy", default=None,
help="Optional http proxy (def: None)")
get_list.add_argument("--httpsproxy", default=None,
help="Optional https proxy (def: None)")
get_list.set_defaults(func=get_sent_list)
# Parse arguments
args = parser.parse_args()
if args != argparse.Namespace():
args.func(args)
else:
parser.print_help()
if __name__ == "__main__":
main()