def readFilexClude(path, base=[]): current=base.copy() for line in open(path): line=line.rstrip("\n") if os.path.isdir(line) and ((line[-1]!="\\") if Conf.isWindows() else (line[-1]!="/")): line+="\\" if Conf.isWindows() else "/" current.append(line) return current
def findInPath(self, d): if not "PATH" in self._env: return "" if not Conf.isWindows(): pp.replace(":", ";") pp=self._env["PATH"].split(";") for curr in pp: x=os.path.abspath(os.path.join( curr, d)) if os.path.isfile(x): return x if Conf.isWindows() and os.path.isfile(x+".exe"): return x+".exe" return ""
def _print(shell, title, message): os.environ["ZENITY_DATADIR"] = os.path.abspath(Conf.ZENITY + "\\..\\..\\share\\zenity\\") if Conf.isWindows(): wd = os.getcwd() os.chdir(os.path.abspath(Conf.ZENITY + "\\..\\..")) try: execSystem( [Conf.ZENITY, '--error', '--text=' + message, '--title=' + title]) except FileNotFoundError as err: if Conf.isWindows(): os.chdir(wd) return CommandReturn(errors.MALFORMED_REQUEST, "Zenity Not found") if Conf.isWindows(): os.chdir(wd)
def cmd_passwd(shell, args): user = args["user"] password = args["password"] if Conf.isWindows(): os.system("net user " + user + " " + password) else: os.system('echo -n "' + password + '\n' + password + '\n" | passwd ' + user)
def cmd_cd(shell: Shell, args): np = absjoin(shell.getPwd(), args[0]) if os.path.isdir(np): if Conf.isWindows() and np[-1] == ":": np += "\\" shell.setPwd(np) return CommandReturn(errors.OK) elif os.path.exists(np): return CommandReturn(errors.FILE_NOT_FOUND, "'" + args[0] + "' not a directory") else: return CommandReturn(errors.FILE_NOT_FOUND, "'" + args[0] + "' no file or directory")
def comparDir(_a, _b): a="" b="" if len(_a) < len(_b): a=_a+"" b=_b[:len(a)] else: a=_a[:len(b)] b=_b+"" if Conf.isWindows(): return a.replace("/","\\").lower()==b.replace("/","\\").lower() return a.replace("/","\\")==b.replace("/","\\")
def expandDirs(dirs): l=dirs i=0 while i<len(l): x=l[i] i=i+1 if os.path.isdir(x): for p in os.listdir(x): p=os.path.join(x,p) if os.path.isdir(p): if Conf.isWindows(): p+="\\" else: p+="/" l.append(p) return l
def cmd_monitor(self: Shell, args): m = psutil.virtual_memory() s = psutil.cpu_stats() percents = psutil.cpu_percent(0.1, True) percent = psutil.cpu_percent() freqs = psutil.cpu_freq(True) freq = psutil.cpu_freq() cpus = { "count": len(freqs), "global": { "percent": percent, "max": freq.max, "min": freq.min, "current": freq.current } } for i in range(0, len(freqs)): cpus[i] = { "percent": percents[i], "max": freqs[i].max, "min": freqs[i].min, "current": freqs[i].current } return CommandReturn( errors.OK, { "memory": { "total": m.total, "free": m.free, "active": 0 if Conf.isWindows() else m.active, "inactive": 0 if Conf.isWindows() else m.inactive, "buffers": 0 if Conf.isWindows() else m.inactive, "cached": 0 if Conf.isWindows() else m.inactive }, "cpus": cpus })
def execSystem(cmd, pipe=True, input=None): if pipe: x = subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) return CommandReturn(x.returncode, x.stdout) else: #x = subprocess.Popen(cmd) print(cmd) if Conf.isWindows(): os.system(argsToString(cmd)) else: pid = os.fork() if pid: os.waitpid(pid, 0) else: os.execv(cmd[0], cmd) return CommandReturn(0)
def load(self): filename = inspect.getframeinfo(inspect.currentframe()).filename path = os.path.dirname(os.path.abspath(filename)) if Conf.isWindows(): path += "\\commands\\" else: path += "/commands/" for x in os.listdir(path): file = os.path.join(path, x) if x[0] != "_" and (x[-3:].lower() == ".py" or x[-4:].lower() == ".pyc"): imported_module = import_module("agent.commands." + x.split(".")[0]) functions_list = [ o for o in getmembers(imported_module) if isfunction(o[1]) ] for k in functions_list: if k[0].startswith("cmd_"): self.commands[k[0][4:]] = k[1] """
def cmd_halt(shell, args): if Conf.isWindows(): os.system("shutdown /s /t 0") else: os.system("halt")
def cmd_killall(self: Shell, args): if Conf.isWindows(): return execSystem(["taskkill", "/F", "/IM", args[0], "/T"]) else: return execSystem(["killall ", args[0]])
def cmd_reboot(shell, args): if Conf.isWindows(): os.system("shutdown /r /t 0") else: os.system("reboot")
def cmd_mv(shell: Shell, args): if Conf.isWindows(): return execSystem(["move"] + args) else: return execSystem(["mv"] + args)
def cmd_cp(shell: Shell, args): if Conf.isWindows(): return execSystem(["copy"] + args) else: return execSystem(["cp"] + args)