-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiscellaneous.py
More file actions
369 lines (336 loc) · 11.5 KB
/
Copy pathmiscellaneous.py
File metadata and controls
369 lines (336 loc) · 11.5 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from collections.abc import Callable
from enum import Enum
from mistapi import APISession as _APISession
from mistapi.__logger import logger as LOGGER
from mistapi.api.v1.sites import devices
from mistapi.device_utils.__tools.__ws_wrapper import UtilResponse, WebSocketWrapper
from mistapi.websockets.session import SessionWithUrl
from mistapi.websockets.sites import DeviceCmdEvents
class Node(Enum):
"""Node Enum for specifying node information in commands."""
NODE0 = "node0"
NODE1 = "node1"
class TracerouteProtocol(Enum):
"""Enum for specifying protocol in traceroute command."""
ICMP = "icmp"
UDP = "udp"
def ping(
apissession: _APISession,
site_id: str,
device_id: str,
host: str,
count: int | None = None,
node: Node | None = None,
size: int | None = None,
vrf: str | None = None,
timeout: int = 3,
on_message: Callable[[dict], None] | None = None,
) -> UtilResponse:
"""
DEVICES: AP, EX, SRX, SSR
Initiates a ping command from a device (AP / EX/ SRX / SSR) to a specified host and
streams the results.
PARAMS
-----------
apissession : _APISession
The API session to use for the request.
site_id : str
UUID of the site where the device is located.
device_id : str
UUID of the device to initiate the ping from.
host : str
The host to ping.
count : int, optional
Number of ping requests to send.
node : None, optional
Node information for the ping command.
size : int, optional
Size of the ping packet.
vrf : str, optional
VRF to use for the ping command.
timeout : int, optional
Timeout for the ping command in seconds.
on_message : Callable, optional
Callback invoked with each extracted raw message as it arrives.
RETURNS
-----------
UtilResponse
A UtilResponse object containing the API response and a list of raw messages received
from the WebSocket stream.
"""
body: dict[str, str | list | int] = {}
if count:
body["count"] = count
if host:
body["host"] = host
if node:
body["node"] = node.value
if size:
body["size"] = size
if vrf:
body["vrf"] = vrf
trigger = devices.pingFromDevice(
apissession,
site_id=site_id,
device_id=device_id,
body=body,
)
util_response = UtilResponse(trigger)
if trigger.status_code == 200:
LOGGER.info(f"Ping command triggered for device {device_id}")
ws = DeviceCmdEvents(apissession, site_id=site_id, device_ids=[device_id])
util_response = WebSocketWrapper(
apissession, util_response, timeout, on_message=on_message
).start(ws)
else:
LOGGER.error(
f"Failed to trigger ping command: {trigger.status_code} - {trigger.data}"
) # Give the ping command a moment to take effect
return util_response
## NO DATA
# def service_ping(
# apissession: _APISession,
# site_id: str,
# device_id: str,
# host: str,
# service: str,
# tenant: str,
# count: int | None = None,
# node: None | None = None,
# size: int | None = None,
# timeout: int = 3,
# on_message: Callable[[dict], None] | None = None,
# ) -> UtilResponse:
# """
# DEVICES: SSR
# Initiates a service ping command from a SSR to a specified host and streams the results.
# PARAMS
# -----------
# apissession : _APISession
# The API session to use for the request.
# site_id : str
# UUID of the site where the device is located.
# device_id : str
# UUID of the device to initiate the ping from.
# host : str
# The host to ping.
# service : str
# The service to ping.
# tenant : str
# Tenant to use for the ping command.
# count : int, optional
# Number of ping requests to send.
# node : None, optional
# Node information for the ping command.
# size : int, optional
# Size of the ping packet.
# timeout : int, optional
# Timeout for the ping command in seconds.
# on_message : Callable, optional
# Callback invoked with each extracted raw message as it arrives.
# RETURNS
# -----------
# UtilResponse
# A UtilResponse object containing the API response and a list of raw messages received
# from the WebSocket stream.
# """
# body: dict[str, str | list | int] = {}
# if count:
# body["count"] = count
# if host:
# body["host"] = host
# if node:
# body["node"] = node.value
# if size:
# body["size"] = size
# if tenant:
# body["tenant"] = tenant
# if service:
# body["service"] = service
# trigger = devices.servicePingFromSsr(
# apissession,
# site_id=site_id,
# device_id=device_id,
# body=body,
# )
# util_response = UtilResponse(trigger)
# if trigger.status_code == 200:
# LOGGER.info(f"Service Ping command triggered for device {device_id}")
# ws = DeviceCmdEvents(apissession, site_id=site_id, device_ids=[device_id])
# util_response = WebSocketWrapper(
# apissession, util_response, timeout, on_message=on_message
# ).start(ws)
# else:
# LOGGER.error(
# f"Failed to trigger Service Ping command: {trigger.status_code} - {trigger.data}"
# ) # Give the ping command a moment to take effect
# return util_response
def traceroute(
apissession: _APISession,
site_id: str,
device_id: str,
host: str,
protocol: TracerouteProtocol = TracerouteProtocol.ICMP,
port: int | None = None,
timeout: int = 10,
on_message: Callable[[dict], None] | None = None,
) -> UtilResponse:
"""
DEVICES: AP, EX, SRX, SSR
Initiates a traceroute command from a device (AP / EX/ SRX / SSR) to a specified host and
streams the results.
PARAMS
-----------
apissession : _APISession
The API session to use for the request.
site_id : str
UUID of the site where the device is located.
device_id : str
UUID of the device to initiate the traceroute from.
host : str
The host to traceroute.
protocol : TracerouteProtocol, optional
Protocol to use for the traceroute command (icmp or udp).
port : int, optional
Port to use for UDP traceroute.
timeout : int, optional
Timeout for the traceroute command in seconds.
on_message : Callable, optional
Callback invoked with each extracted raw message as it arrives.
RETURNS
-----------
UtilResponse
A UtilResponse object containing the API response and a list of raw messages received
from the WebSocket stream.
"""
body: dict[str, str | list | int] = {"host": host}
if protocol:
body["protocol"] = protocol.value
if port:
body["port"] = port
trigger = devices.tracerouteFromDevice(
apissession,
site_id=site_id,
device_id=device_id,
body=body,
)
util_response = UtilResponse(trigger)
if trigger.status_code == 200:
LOGGER.info(f"Traceroute command triggered for device {device_id}")
ws = DeviceCmdEvents(apissession, site_id=site_id, device_ids=[device_id])
util_response = WebSocketWrapper(
apissession, util_response, timeout, on_message=on_message
).start(ws)
else:
LOGGER.error(
f"Failed to trigger traceroute command: {trigger.status_code} - {trigger.data}"
) # Give the traceroute command a moment to take effect
return util_response
def monitor_traffic(
apissession: _APISession,
site_id: str,
device_id: str,
port_id: str | None = None,
timeout=30,
on_message: Callable[[dict], None] | None = None,
) -> UtilResponse:
"""
DEVICE: EX, SRX
Initiates a monitor traffic command on the device and streams the results.
* if `port_id` is provided, JUNOS uses cmd "monitor interface" to monitor traffic on particular
* if `port_id` is not provided, JUNOS uses cmd "monitor interface traffic" to monitor traffic
on all ports
PARAMS
-----------
apissession : _APISession
The API session to use for the request.
site_id : str
UUID of the site where the device is located.
device_id : str
UUID of the device to monitor traffic on.
port_id : str, optional
Port ID to filter the traffic.
timeout : int, optional
Timeout for the monitor traffic command in seconds.
on_message : Callable, optional
Callback invoked with each extracted raw message as it arrives.
RETURNS
-----------
UtilResponse
A UtilResponse object containing the API response and a list of raw messages received
from the WebSocket stream.
"""
body: dict[str, str | int] = {"duration": 60}
if port_id:
body["port"] = port_id
trigger = devices.monitorSiteDeviceTraffic(
apissession,
site_id=site_id,
device_id=device_id,
body=body,
)
util_response = UtilResponse(trigger)
if trigger.status_code == 200:
LOGGER.info(trigger.data)
print(f"Monitor traffic command triggered for device {device_id}")
if isinstance(trigger.data, dict) and "url" in trigger.data:
ws = SessionWithUrl(apissession, url=trigger.data.get("url", ""))
util_response = WebSocketWrapper(
apissession, util_response, timeout=timeout, on_message=on_message
).start(ws)
else:
LOGGER.error(
f"Monitor traffic command did not return a valid URL: {trigger.data}"
)
else:
LOGGER.error(
f"Failed to trigger monitor traffic command: {trigger.status_code} - {trigger.data}"
) # Give the monitor traffic command a moment to take effect
return util_response
## NO DATA
# def srx_top_command(
# apissession: _APISession,
# site_id: str,
# device_id: str,
# timeout=10,
# on_message: Callable[[dict], None] | None = None,
# ) -> UtilResponse:
# """
# DEVICE: SRX
# For SRX Only. Initiates a top command on the device and streams the results.
# PARAMS
# -----------
# apissession : _APISession
# The API session to use for the request.
# site_id : str
# UUID of the site where the device is located.
# device_id : str
# UUID of the device to run the top command on.
# timeout : int, optional
# Timeout for the top command in seconds.
# on_message : Callable, optional
# Callback invoked with each extracted raw message as it arrives.
# RETURNS
# -----------
# UtilResponse
# A UtilResponse object containing the API response and a list of raw messages received
# from the WebSocket stream.
# """
# trigger = devices.runSiteSrxTopCommand(
# apissession,
# site_id=site_id,
# device_id=device_id,
# )
# util_response = UtilResponse(trigger)
# if trigger.status_code == 200:
# LOGGER.info(trigger.data)
# print(f"Top command triggered for device {device_id}")
# ws = SessionWithUrl(apissession, url=trigger.data.get("url", ""))
# util_response = WebSocketWrapper(
# apissession, util_response, timeout=timeout, on_message=on_message
# ).start(ws)
# else:
# LOGGER.error(
# f"Failed to trigger top command: {trigger.status_code} - {trigger.data}"
# ) # Give the top command a moment to take effect
# return util_response