Repository navigation
Expand file tree
/
Copy pathxapi_ethernet_mics.py
More file actions
306 lines (244 loc) · 10.6 KB
/
Copy pathxapi_ethernet_mics.py
File metadata and controls
306 lines (244 loc) · 10.6 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
#!/usr/bin/env python3
# Copyright (C) 2026 Frederick W. Nielsen
#
# This file is part of xAPI tools.
#
# xAPI tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# xAPI tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with xAPI tools. If not, see <https://www.gnu.org/licenses/>.
"""
xapi_ethernet_mics.py
Enumerate ethernet microphone input streams on a Cisco RoomOS codec.
Queries two xStatus paths:
1. Peripherals ConnectedDevice - serial, MAC, type, name, connection status
(filtered to microphone types)
2. Audio Input Connectors Ethernet - stream-level detail for ethernet audio inputs
Modes:
local - SSH into device and run xStatus
cloud - Webex Cloud xAPI REST
Deps:
pip install paramiko requests pyyaml
"""
from __future__ import annotations
import argparse
import getpass
import json
import re
import sys
from typing import Any, Dict, List, Optional
from xapi_common import resolve_device_id, resolve_token, ssh_run_xcommands, xapi_status
# Matches xStatus response lines like:
# *s Peripherals ConnectedDevice 1 Name: "Cisco Table Microphone"
STATUS_LINE_RE = re.compile(r"^\*s\s+(?P<path>.+?):\s+(?P<val>.*)\s*$")
def _clean_value(v: str) -> str:
"""Strip whitespace and surrounding quotes from an xStatus value."""
v = v.strip()
if len(v) >= 2 and v[0] == '"' and v[-1] == '"':
v = v[1:-1]
return v
# ------------------------------------------------------------------
# Parsing xStatus output (local mode)
# ------------------------------------------------------------------
def parse_peripherals(raw: str) -> List[Dict[str, str]]:
"""Parse *s Peripherals ConnectedDevice lines into a list of device dicts."""
device_re = re.compile(
r"^Peripherals\s+ConnectedDevice\s+(?P<idx>\d+)\s+(?P<prop>.+)$"
)
devices: Dict[int, Dict[str, str]] = {}
for line in raw.splitlines():
m = STATUS_LINE_RE.match(line)
if not m:
continue
path = m.group("path").strip()
val = _clean_value(m.group("val"))
dm = device_re.match(path)
if dm:
idx = int(dm.group("idx"))
prop = dm.group("prop").strip()
devices.setdefault(idx, {})[prop] = val
# Filter to microphone types
mics = []
for idx in sorted(devices):
dev = devices[idx]
dev_type = dev.get("Type", "")
dev_name = dev.get("Name", "")
if "microphone" in dev_type.lower() or "microphone" in dev_name.lower():
dev["_index"] = str(idx)
mics.append(dev)
return mics
def parse_ethernet_inputs(raw: str) -> List[Dict[str, str]]:
"""Parse *s Audio Input Connectors Ethernet lines into a list of connector dicts."""
connector_re = re.compile(
r"^Audio\s+Input\s+Connectors\s+Ethernet\s+(?P<idx>\d+)\s+(?P<prop>.+)$"
)
connectors: Dict[int, Dict[str, str]] = {}
for line in raw.splitlines():
m = STATUS_LINE_RE.match(line)
if not m:
continue
path = m.group("path").strip()
val = _clean_value(m.group("val"))
cm = connector_re.match(path)
if cm:
idx = int(cm.group("idx"))
prop = cm.group("prop").strip()
connectors.setdefault(idx, {})[prop] = val
return [connectors[idx] for idx in sorted(connectors)]
# ------------------------------------------------------------------
# Cloud mode: Webex xAPI REST
# ------------------------------------------------------------------
def cloud_get_status(device_id: str, token: str, name: str,
base_url: str, timeout: int) -> Dict[str, Any]:
"""GET /v1/xapi/status?deviceId=...&name=..."""
return xapi_status(name, device_id, token, base_url, timeout)
def extract_cloud_peripherals(data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Extract microphone peripherals from cloud status response."""
result = data.get("result", data)
peripherals = result.get("Peripherals", result)
devices_raw = peripherals.get("ConnectedDevice", [])
if isinstance(devices_raw, dict):
devices_raw = [devices_raw]
mics = []
for dev in devices_raw:
if not isinstance(dev, dict):
continue
dev_type = str(dev.get("Type", ""))
dev_name = str(dev.get("Name", ""))
if "microphone" in dev_type.lower() or "microphone" in dev_name.lower():
mics.append(dev)
return mics
def extract_cloud_ethernet_inputs(data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Extract ethernet audio input connectors from cloud status response."""
result = data.get("result", data)
audio = result.get("Audio", result)
inputs = audio.get("Input", audio)
connectors = inputs.get("Connectors", inputs)
ethernet_raw = connectors.get("Ethernet", [])
if isinstance(ethernet_raw, dict):
ethernet_raw = [ethernet_raw]
return [e for e in ethernet_raw if isinstance(e, dict)]
# ------------------------------------------------------------------
# Display helpers
# ------------------------------------------------------------------
def print_table(title: str, items: List[Dict[str, Any]], key_order: Optional[List[str]] = None) -> None:
"""Print a list of dicts as a readable table."""
if not items:
print(f"\n{title}: (none found)")
return
print(f"\n{title}:")
print("-" * len(title))
for i, item in enumerate(items):
print(f"\n [{i + 1}]")
keys = key_order if key_order else sorted(item.keys())
for k in keys:
if k in item:
print(f" {k}: {item[k]}")
if key_order:
for k in sorted(item.keys()):
if k not in key_order:
print(f" {k}: {item[k]}")
# ------------------------------------------------------------------
# CLI
# ------------------------------------------------------------------
def main() -> int:
"""list ethernet microphone inputs on a RoomOS codec (local SSH or cloud xAPI)"""
ap = argparse.ArgumentParser(
description="Enumerate ethernet microphone input streams on a RoomOS codec "
"via local SSH or Webex Cloud xAPI.",
)
sub = ap.add_subparsers(dest="mode", required=True)
# ---- local subparser ----
ap_local = sub.add_parser("local", help="Query via local SSH xAPI")
ap_local.add_argument("--host", required=True, help="Codec IP/hostname")
ap_local.add_argument("-P", "--port", type=int, default=22, help="SSH port (default: 22)")
ap_local.add_argument("-u", "--username", required=True, help="SSH username")
ap_local.add_argument("-p", "--password", help="SSH password (omit to prompt)")
ap_local.add_argument("-k", "--key", dest="key_path", help="SSH private key path (optional)")
ap_local.add_argument("--timeout", type=int, default=10, help="SSH timeout seconds (default: 10)")
# ---- cloud subparser ----
ap_cloud = sub.add_parser("cloud", help="Query via Webex Cloud xAPI REST")
ap_cloud.add_argument("--device-id", help="Webex deviceId of the codec (or set XAPI_DEVICE_ID)")
ap_cloud.add_argument("--token", help="Webex access token (omit to prompt)")
ap_cloud.add_argument("--base-url", default="https://webexapis.com", help="Webex API base URL")
ap_cloud.add_argument("--timeout", type=int, default=15, help="HTTP timeout seconds (default: 15)")
# ---- shared args ----
for p in (ap_local, ap_cloud):
p.add_argument("--json", action="store_true", help="Output as JSON")
args = ap.parse_args()
try:
# ---- Local mode ----
if args.mode == "local":
if not args.password and not args.key_path:
args.password = getpass.getpass("SSH Password: ")
raw = ssh_run_xcommands(
host=args.host, port=args.port,
username=args.username, password=args.password,
key_path=args.key_path,
commands=[
"xStatus Peripherals ConnectedDevice",
"xStatus Audio Input Connectors Ethernet",
],
timeout=args.timeout,
)
mics = parse_peripherals(raw)
eth_inputs = parse_ethernet_inputs(raw)
if args.json:
print(json.dumps({"peripherals": mics, "ethernet_inputs": eth_inputs}, indent=2))
else:
print_table(
"Ethernet Microphone Peripherals", mics,
key_order=["Name", "Type", "SerialNumber", "HardwareInfo", "Status"],
)
print_table(
"Audio Input Connectors (Ethernet)", eth_inputs,
)
return 0
# ---- Cloud mode ----
if args.mode == "cloud":
token = resolve_token(args.token)
device_id = resolve_device_id(args.device_id)
if not device_id:
print("ERROR: device id required: pass --device-id or set XAPI_DEVICE_ID",
file=sys.stderr)
return 2
periph_data = cloud_get_status(
device_id=device_id, token=token,
name="Peripherals.ConnectedDevice[*].*",
base_url=args.base_url, timeout=args.timeout,
)
eth_data = cloud_get_status(
device_id=device_id, token=token,
name="Audio.Input.Connectors.Ethernet[*].*",
base_url=args.base_url, timeout=args.timeout,
)
mics = extract_cloud_peripherals(periph_data)
eth_inputs = extract_cloud_ethernet_inputs(eth_data)
if args.json:
print(json.dumps({"peripherals": mics, "ethernet_inputs": eth_inputs}, indent=2))
else:
print_table(
"Ethernet Microphone Peripherals", mics,
key_order=["Name", "Type", "SerialNumber", "HardwareInfo", "Status"],
)
print_table(
"Audio Input Connectors (Ethernet)", eth_inputs,
)
return 0
return 2
except KeyboardInterrupt:
print("\nCancelled.", file=sys.stderr)
return 130
except Exception as e:
print(f"ERROR: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())