import xmlrpclib import tempfile import os import os.path import stat import time import subprocess class ClientExeConsumer: LOG_FILE="/var/log/n4d/client-consumer" def read_log(self): self.md5_log=[] try: f=open(ClientExeConsumer.LOG_FILE) lines=f.readlines() f.close() except: lines=[] try: f=open(ClientExeConsumer.LOG_FILE,"w") f.close() except: return -1 for item in lines: line=item.strip("\n") if len(line)>0: self.md5_log.append(line) #def read_log def get_exes(self): ret_list=[] try: srv=objects["VariablesManager"].get_variable("CLIENT_EXE_SERVER") if srv!=None and type(srv)==type(""): c=xmlrpclib.ServerProxy("https://"+srv+":9779") ret=c.get_available_exes("","ClientExeManager",self.md5_log) if type(ret)==type([]): for item in ret: md5,content=item content=content.encode("utf-8") id,file_name=tempfile.mkstemp(prefix="n4d-cec-") os.close(id) st=os.stat(file_name) os.chmod(file_name,st.st_mode | stat.S_IEXEC) script=open(file_name,"w") script.write(content) script.close() ret_list.append(file_name) self.add_md5(md5) except Exception as e: print e pass return ret_list #def get_exes def add_md5(self,md5): self.read_log() if md5 not in self.md5_log: f=open(ClientExeConsumer.LOG_FILE,"a") f.write(md5+"\n") f.close() self.md5_log.append(md5) def execute_and_delete(self,file_list,delete=True): for item in file_list: print ("[ClientExeConsumer] Executing " + item +" ...") os.system(item) if delete: os.remove(item) def startup(self,options): if not self.check_semiclient(): if objects['VariablesManager'].get_variable('CLIENT_EXE_SERVER') is None: objects['VariablesManager'].init_variable('CLIENT_EXE_SERVER') self.read_log() ret=self.get_exes() self.execute_and_delete(ret) #def startup def check_semiclient(self): return os.path.exists("/etc/lts.conf") #def check_semiclient def force_execution(self,force_all=False): if not self.check_semiclient(): try: if force_all: self.md5_log=[] ret=self.get_exes() self.execute_and_delete(ret) if force_all: self.read_log() return [True,""] except Exception as e: print(e) return [False,str(e)] else: return [False,"Semiclient found"] #def force_execution #class ClientExeConsumer if __name__=="__main__": cec=ClientExeConsumer() cec.startup(None)