2

I'm creating a python 3 tool that requires the access of many other preexisting networking tools, and some don't have python libraries. I've tried subprocess.check_output, which works, however, this data is not easy to work with, and I don't believe it supports piping commands like grep or awk if both commands require arguments. I want to be able to, on separate threads, run each command and store the output, as well as stop the output of the command, as I'll be using tools like SSLStrip, ArpSpoof, nmap, and others. that run until terminated.

What's the best way to do this?

Tessa Painter
  • 2,113
  • 2
  • 16
  • 21
  • `subprocess` has many mechanisms for running commands and retrieving their output (e.g. `Popen`). You can also connect a process' output to another process' input (haven't ever run into an issue with command-line arguments). That said, is there a reason you're not using a shell script to tie everything together? It might be simpler in the long run... – bedwyr Jan 25 '18 at 15:34
  • I can imagine OP uses Python, since in my perception it is much more powerful than any shell script language. But of course that's subjective. What I also like is that I can run such scripts on Windows, OsX and Linux, modifying only the calls to system-specific tools or commands. – Jacques de Hooge Jan 25 '18 at 15:39
  • The upstream Python documentation **explicitly** tells you how to set up pipelines with `subprocess`. See https://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline – Charles Duffy Jan 25 '18 at 15:57
  • Moreover, if you create multiple `Popen` processes, then you have multiple programs running on separate processes already, with the ability to individually stop them &c -- so that too is already available out-of-the-box with no extra work needed. If you have a very specific, narrow question, please ask it individually, with a detailed description of the specific behavior you want, some code showing what you're trying now, a description of how its current behavior differs from your desired behavior, etc. – Charles Duffy Jan 25 '18 at 15:58
  • ("Not easy to work with" doesn't tell us anything about *why* you're finding something hard to work with, and thus about what the specific issue you're having is. We're not mind readers here, and you can't assume that everyone else has the same needs, and thus the same problems, that you're running into yourself). – Charles Duffy Jan 25 '18 at 15:59

1 Answers1

1

I propose to use subprocess to build proper pipes as you are used to in shell syntax. It just gets a little more "syntaxish". Here's an example of how to call ls | tr e E in Python:

from subprocess import Popen, PIPE

ls = Popen([ 'ls' ], stdout=PIPE)
tr = Popen([ 'tr', 'e', 'E' ], stdin=ls.stdout, stdout=PIPE)
out, err = tr.communicate()

After this, out will contain the output of ls with all the e translated to E.

Of course, more complex stuff can be done using this method.

Alfe
  • 56,346
  • 20
  • 107
  • 159