|
|
|
@ -1,9 +1,72 @@
|
|
|
|
|
import threading, socket, getpass
|
|
|
|
|
import threading, socket, getpass, sys, uuid, subprocess
|
|
|
|
|
from . import O2A, A2O # Data flow Orchestrator To Agent
|
|
|
|
|
from . import Processor # Processor Queue
|
|
|
|
|
|
|
|
|
|
# Send CMD to OS. Result return to log + Orchestrator by the A2O connection
|
|
|
|
|
def OSFileBytesCreate(inFilePathStr, inFileBytes,inGSettings = None):
|
|
|
|
|
lFile = open(inFilePathStr, "wb")
|
|
|
|
|
lFile.write(inFileBytes)
|
|
|
|
|
lL = inGSettings.get("Logger", None) if type(inGSettings) is dict else None
|
|
|
|
|
lMessageStr = f"File {inFilePathStr} has been created."
|
|
|
|
|
if lL: lL.info(lMessageStr)
|
|
|
|
|
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
|
|
|
|
|
|
|
|
|
|
# Send CMD to OS. Result return to log + Orchestrator by the A2O connection
|
|
|
|
|
def OSCMD(inCMDStr, inRunAsyncBool=True, inGSettings = None):
|
|
|
|
|
lResultStr = ""
|
|
|
|
|
# Subdef to listen OS result
|
|
|
|
|
def _CMDRunAndListenLogs(inCMDStr, inGSettings = None):
|
|
|
|
|
lL = inGSettings.get("Logger",None) if type(inGSettings) is dict else None
|
|
|
|
|
lResultStr = ""
|
|
|
|
|
lOSCMDKeyStr = str(uuid.uuid4())[0:4].upper()
|
|
|
|
|
lCMDProcess = subprocess.Popen(f'cmd /c {inCMDStr}', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
|
|
|
lListenBool = True
|
|
|
|
|
lMessageStr = f"{lOSCMDKeyStr}: # # # # CMD Process has been STARTED # # # # "
|
|
|
|
|
if lL: lL.info(lMessageStr)
|
|
|
|
|
A2O.LogListSend(inGSettings=inGSettings,inLogList=[lMessageStr])
|
|
|
|
|
lMessageStr = f"{lOSCMDKeyStr}: {inCMDStr}"
|
|
|
|
|
if lL: lL.info(lMessageStr)
|
|
|
|
|
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
|
|
|
|
|
while lListenBool:
|
|
|
|
|
lOutputLineBytes = lCMDProcess.stdout.readline()
|
|
|
|
|
if lOutputLineBytes == b"":
|
|
|
|
|
lListenBool = False
|
|
|
|
|
lStr = lOutputLineBytes.decode('cp866')
|
|
|
|
|
if lStr.endswith("\n"): lStr = lStr[:-1]
|
|
|
|
|
lMessageStr = f"{lOSCMDKeyStr}: {lStr}"
|
|
|
|
|
if lL: lL.info(lMessageStr)
|
|
|
|
|
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
|
|
|
|
|
lResultStr+=lStr
|
|
|
|
|
lMessageStr = f"{lOSCMDKeyStr}: # # # # CMD Process has been FINISHED # # # # "
|
|
|
|
|
if lL: lL.info(lMessageStr)
|
|
|
|
|
A2O.LogListSend(inGSettings=inGSettings, inLogList=[lMessageStr])
|
|
|
|
|
return lResultStr
|
|
|
|
|
# New call
|
|
|
|
|
if inRunAsyncBool:
|
|
|
|
|
lThread = threading.Thread(target=_CMDRunAndListenLogs, kwargs={"inCMDStr":inCMDStr, "inGSettings":inGSettings})
|
|
|
|
|
lThread.start()
|
|
|
|
|
lResultStr="ActivityList has been started in async mode - no output is available here."
|
|
|
|
|
else:
|
|
|
|
|
lResultStr = _CMDRunAndListenLogs(inCMDStr=inCMDStr, inGSettings=inGSettings)
|
|
|
|
|
#lCMDCode = "cmd /c " + inCMDStr
|
|
|
|
|
#subprocess.Popen(lCMDCode)
|
|
|
|
|
#lResultCMDRun = 1 # os.system(lCMDCode)
|
|
|
|
|
return lResultStr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Main def
|
|
|
|
|
def Agent(inGSettings):
|
|
|
|
|
lL = inGSettings["Logger"]
|
|
|
|
|
|
|
|
|
|
# Append Orchestrator def to ProcessorDictAlias
|
|
|
|
|
lModule = sys.modules[__name__]
|
|
|
|
|
lModuleDefList = dir(lModule)
|
|
|
|
|
for lItemDefNameStr in lModuleDefList:
|
|
|
|
|
# Dont append alias for defs Agent
|
|
|
|
|
if lItemDefNameStr not in ["Agent"]:
|
|
|
|
|
lItemDef = getattr(lModule,lItemDefNameStr)
|
|
|
|
|
if callable(lItemDef): inGSettings["ProcessorDict"]["AliasDefDict"][lItemDefNameStr]=lItemDef
|
|
|
|
|
|
|
|
|
|
# Detect Machine host name and username
|
|
|
|
|
inGSettings["AgentDict"]["HostNameUpperStr"] = socket.gethostname().upper()
|
|
|
|
|
inGSettings["AgentDict"]["UserUpperStr"] = getpass.getuser().upper()
|
|
|
|
|