Skip to content

Commit 6290bc7

Browse files
committed
Fixed ignore_output for run_command
1 parent 5975e14 commit 6290bc7

1 file changed

Lines changed: 27 additions & 24 deletions

File tree

src/meshctrl/session.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ def match_nodeid(id, ids):
15011501
if (f"node//{nid}" == id):
15021502
return nid
15031503

1504-
result = None
1504+
result = {n: {"complete": False, "result": [], "command": command} for n in nodeids}
15051505
console_result = {n: {"complete": False, "result": [], "command": command} for n in nodeids}
15061506
reply_result = {n: {"complete": False, "result": [], "command": command} for n in nodeids}
15071507
async def _console():
@@ -1536,49 +1536,52 @@ def _parse_event(event):
15361536
async def __(command, tg, tasks):
15371537
nonlocal result
15381538
responseid = self._generate_response_id("run_command")
1539+
15391540
if not ignore_output:
15401541
reply_task = tg.create_task(asyncio.wait_for(_reply(responseid), timeout=timeout))
1541-
console_task = tg.create_task(asyncio.wait_for(_console(), timeout=timeout))
1542+
# We still need to parse the console results because it sends them without namespace, this will likely break older versions of meshcentral
1543+
console_task = tg.create_task(asyncio.wait_for(_console(), timeout=timeout))
15421544
data = await self._send_command(command, "run_command", timeout=timeout, responseid=responseid)
15431545

15441546
if data.get("type", None) != "runcommands" and data.get("result", "ok").lower() != "ok":
15451547
raise exceptions.ServerError(data["result"])
15461548
elif data.get("type", None) != "runcommands" and data.get("result", "ok").lower() == "ok":
1547-
reply_task.cancel()
1548-
result = console_result
15491549
expect_response = False
15501550
if not ignore_output:
1551-
userid = (await self.user_info())["_id"]
1552-
for n in nodeids:
1553-
device_info = await self.device_info(n, timeout=timeout)
1554-
try:
1555-
permissions = device_info.mesh.links.get(userid, {}).get("rights",constants.DeviceRights.norights)\
1556-
# This should work for device rights, but it only seems to work for mesh rights. Not sure why, but I can't get the events to show up when the user only has individual device rights
1557-
# |device_info.get("links", {}).get(userid, {}).get("rights", constants.DeviceRights.norights)
1558-
# If we don't have agentconsole rights, we won't be able te read the output, so fill in blanks on this node
1559-
if not permissions&constants.DeviceRights.agentconsole:
1560-
result[n]["complete"] = True
1561-
else:
1562-
expect_response = True
1563-
except AttributeError:
1551+
reply_task.cancel()
1552+
result = console_result
1553+
userid = (await self.user_info())["_id"]
1554+
for n in nodeids:
1555+
device_info = await self.device_info(n, timeout=timeout)
1556+
try:
1557+
permissions = device_info.mesh.links.get(userid, {}).get("rights",constants.DeviceRights.norights)
1558+
# This should work for device rights, but it only seems to work for mesh rights. Not sure why, but I can't get the events to show up when the user only has individual device rights
1559+
# |device_info.get("links", {}).get(userid, {}).get("rights", constants.DeviceRights.norights)
1560+
# If we don't have agentconsole rights, we won't be able te read the output, so fill in blanks on this node
1561+
if not permissions&constants.DeviceRights.agentconsole:
15641562
result[n]["complete"] = True
1563+
else:
1564+
expect_response = True
1565+
except AttributeError:
1566+
result[n]["complete"] = True
15651567
if expect_response:
15661568
tasks.append(console_task)
15671569
else:
15681570
console_task.cancel()
1569-
elif data.get("type", None) == "runcommands" and not ignore_output:
1570-
result = reply_result
1571+
elif data.get("type", None) == "runcommands":
15711572
console_task.cancel()
1572-
tasks.append(reply_task)
1573-
else:
15741573
if not ignore_output:
1575-
console_task.cancel()
1576-
reply_task.cancel()
1574+
result = reply_result
1575+
tasks.append(reply_task)
1576+
else:
1577+
# if not ignore_output:
1578+
console_task.cancel()
1579+
reply_task.cancel()
15771580
raise exceptions.ServerError(f"Unrecognized response: {data}")
15781581

15791582
tasks = []
15801583
async with asyncio.TaskGroup() as tg:
1581-
tasks.append(tg.create_task(__({ "action": 'runcommands', "nodeids": nodeids, "type": (2 if powershell else 0), "cmds": command, "runAsUser": runAsUser, "reply": not ignore_output}, tg, tasks)))
1584+
tasks.append(tg.create_task(__({ "action": 'runcommands', "nodeids": nodeids, "type": (2 if powershell else 0), "cmds": command, "runAsUser": runAsUser, "reply": True}, tg, tasks)))
15821585

15831586
return {n: v | {"result": "".join(v["result"])} for n,v in result.items()}
15841587

0 commit comments

Comments
 (0)