2025-04-25 16:03:07 +02:00
|
|
|
# Public Python libraries
|
|
|
|
|
import argparse
|
|
|
|
|
import json
|
|
|
|
|
import meshctrl
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
|
|
# Local Python libraries/modules
|
2025-12-29 10:55:57 +01:00
|
|
|
from modules.console import Console
|
|
|
|
|
from modules.utilities import Transform
|
2025-04-25 16:03:07 +02:00
|
|
|
|
2025-09-16 10:37:13 +02:00
|
|
|
intertask_delay = 1
|
2025-04-25 16:09:36 +02:00
|
|
|
|
2025-12-29 10:55:57 +01:00
|
|
|
class Executor:
|
2025-09-25 09:49:19 +02:00
|
|
|
@staticmethod
|
2025-12-29 10:55:57 +01:00
|
|
|
async def execute_meshbook(silent: bool, enable_shlex: bool, session: meshctrl.Session, compiled_device_list: dict, meshbook: dict, group_list: dict) -> dict:
|
2025-04-25 16:03:07 +02:00
|
|
|
'''
|
|
|
|
|
Actual function that handles meshbook execution, also responsible for formatting the resulting JSON.
|
|
|
|
|
'''
|
2025-11-17 09:01:16 +01:00
|
|
|
|
2025-12-29 10:55:57 +01:00
|
|
|
complete_log = {}
|
2025-04-25 16:03:07 +02:00
|
|
|
targets = compiled_device_list["target_list"]
|
|
|
|
|
offline = compiled_device_list["offline_list"]
|
|
|
|
|
round = 1
|
|
|
|
|
|
|
|
|
|
for task in meshbook["tasks"]:
|
2025-12-29 10:55:57 +01:00
|
|
|
Console.print_text(silent,
|
|
|
|
|
Console.text_color.green + str(round) + ". Running: " + task["name"])
|
2025-04-25 16:03:07 +02:00
|
|
|
|
|
|
|
|
if "powershell" in meshbook and meshbook["powershell"]:
|
2025-11-17 09:01:16 +01:00
|
|
|
response = await session.run_command(nodeids=targets, command=task["command"],powershell=True,ignore_output=False,timeout=1800)
|
2025-04-25 16:03:07 +02:00
|
|
|
else:
|
2025-11-17 09:01:16 +01:00
|
|
|
response = await session.run_command(nodeids=targets, command=task["command"],powershell=False,ignore_output=False,timeout=1800)
|
2025-04-25 16:03:07 +02:00
|
|
|
|
|
|
|
|
task_batch = []
|
|
|
|
|
for device in response:
|
|
|
|
|
device_result = response[device]["result"]
|
|
|
|
|
response[device]["result"] = device_result.replace("Run commands completed.", "")
|
|
|
|
|
response[device]["device_id"] = device
|
2025-12-29 10:55:57 +01:00
|
|
|
response[device]["device_name"] = await Transform.translate_nodeid_to_name(device, group_list)
|
2025-04-25 16:03:07 +02:00
|
|
|
task_batch.append(response[device])
|
|
|
|
|
|
2025-12-29 10:55:57 +01:00
|
|
|
complete_log["task_" + str(round)] = {
|
2025-04-25 16:03:07 +02:00
|
|
|
"task_name": task["name"],
|
|
|
|
|
"data": task_batch
|
|
|
|
|
}
|
|
|
|
|
round += 1
|
2025-04-25 16:09:36 +02:00
|
|
|
sleep(intertask_delay) # Sleep for x amount of time.
|
2025-04-25 16:03:07 +02:00
|
|
|
|
|
|
|
|
for index, device in enumerate(offline): # Replace Device_id with actual human readable name
|
2025-12-29 10:55:57 +01:00
|
|
|
device_name = await Transform.translate_nodeid_to_name(device, group_list)
|
2025-04-25 16:03:07 +02:00
|
|
|
offline[index] = device_name
|
2025-12-29 10:55:57 +01:00
|
|
|
complete_log["Offline"] = offline
|
2025-04-25 16:03:07 +02:00
|
|
|
|
2025-12-29 10:55:57 +01:00
|
|
|
# Return the result
|
|
|
|
|
return Transform.process_shell_response(enable_shlex, complete_log)
|