def _kill_process(process): if stem.prereq.is_python_26(): process.kill() elif not stem.util.system.is_windows(): os.kill(process.pid, signal.SIGTERM) process.communicate() # block until its definitely gone
def execute_process(self, command, shell=True, root=False, loader=None, timeout=5, pid=False): """Handle a process execution returning a tuple with stdout and stderr""" if root: command[0] = 'sudo ' + command[0] logger.log( '[*] Running: ' + (' '.join(command) if type(command) is list else command), YELLOW) process = subprocess.Popen(command, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) try: if root and self._require_root: stdout, stderr = process.communicate() else: stdout, stderr = process.communicate(timeout=timeout) if loader: loader.start() except subprocess.TimeoutExpired: stdout, stderr = (b'', b'Process timed out.') if root and 'incorrect' in stderr.decode().lower(): return '', 'Incorrect password.' output, errors = self._parse_output(stdout, stderr) if pid: return output, errors, process.pid return output, errors
def _execute_command(self, command): process_wrapped = False if command[:2] == "cd": try: directory = command[3:].strip() os.chdir(directory) return os.getcwd() + "\xff\xff" except Exception as e: return f"Not found: {directory}\xff\xff" process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) if "powershell" in command.lower(): process = ProcessExecWrapper(process) process_wrapped = True process.begin() while True: if process_wrapped: proc_output = process.readline() else: proc_output, _ = process.communicate() if proc_output != b"" or process.poll() is None: if proc_output: print(proc_output) self.request.sendall(proc_output) confirmation_message = self.request.recv(2) if not process_wrapped: break if confirmation_message == b"\x01\x01": continue else: print("[!] Invalid confirmation, breaking.") break else: print("Process exited") break if process_wrapped: process.end() time.sleep(1) return f"Process exited with code: {process.poll()}\xff\xff"
def _kill_process(process): process.kill() process.communicate() # block until its definitely gone