def process_output(self, stdout, output_queue, output_filter=None, process=None, raise_error=False): """Helper function to read from a text stream asynchroneously Arguments: stdout - stream to read from output_queue - queue to append lines to output_filter - regex to filter lines before appending to the queue process - if raise_error, check return code, raising an exception if it's non-zero raise_error - see above """ lines = [] for line in stdout: if type(line) is bytes: line = line.decode() lines.append(line) if output_filter: match = re.search(output_filter, line) if match: output_queue.put(match.group(1)) continue output_queue.put(line) if process and raise_error: exitcode = process.wait() if exitcode != 0: raise subprocess.CalledProcessError(exitcode, process, lines)
def _git(self, cmd, stdin=None): """Executes a git command and returns the standard output.""" p = subprocess.Popen(['git'] + cmd, cwd=self._repo, stdin=subprocess.PIPE, stdout=subprocess.PIPE) stdout, _ = p.communicate(stdin) if p.returncode != 0: raise subprocess.CalledProcessError(p.returncode, ['git'] + cmd, None) return stdout.strip().splitlines()
def ptsqlmap(self, cmd): p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cf.sqlmap_path) output, unused_err = p.communicate() retcode = p.poll() if retcode: raise subprocess.CalledProcessError(retcode, cmd, output=output) return output
def check_call(args): if USE_SU: proc = subprocess.Popen('su', stdin=subprocess.PIPE) proc.stdin.write(' '.join(args)) proc.stdin.write('\nexit\n') proc.communicate() retcode = proc.poll() if retcode: raise subprocess.CalledProcessError(retcode, args) return 0 else: return subprocess.check_call(args)
def check_output(args): if USE_SU: proc = subprocess.Popen('su', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) proc.stdin.write(' '.join(args)) proc.stdin.write('\nexit\n') output = proc.communicate()[0] retcode = proc.poll() if retcode: raise subprocess.CalledProcessError(retcode, args, output=output) return output else: return subprocess.check_output(args, stderr=subprocess.STDOUT)
def _execute(self, *args): """Runs a raw command. Separate so it's easily mockable.""" LOGGER.info('Running: %s', args) process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, stderr = process.communicate() retcode = process.poll() if retcode: if output and stderr: new_output = 'STDOUT\n%s\nSTDERR\n%s' % (output, stderr) else: new_output = output or stderr raise subprocess.CalledProcessError(retcode, args, new_output) return output
def psqlmap(self, cmd): print "sqlmap cmd :" + cmd print "sqlmap path:" + cf.sqlmap_path start = time.time() p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cf.sqlmap_path, bufsize=10000, close_fds=True) #p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd= cf.sqlmap_path, bufsize=100000, close_fds=True) ''' while p.poll() is None: time.sleep(1) now = datetime.datetime.now() print "the sqlmap is running time :"+str((now-start).seconds)+"\tthe poll status is =>"+str(p.poll()) if (now - start).seconds > 60*5: try: p.terminate() p.kill() except Exception,e: elog.exception(e) return "" return "" output, unused_err = p.communicate() print output[:1000] if p.stdin: p.stdin.close() if p.stdout: p.stdout.close() if p.stderr: p.stderr.close() try: p.kill() except OSError: pass return output ''' output, unused_err = p.communicate() retcode = p.poll() if retcode: raise subprocess.CalledProcessError(retcode, cmd, output=output) print "sqlmap runing time=>" + str(time.time() - start) return output