1

I am trying to use the python subprocess to invoke a third party command ParseAddress. The command ParseAddress prints out a line of text asking a user to input a list of addresses, then, based on the input it prints out stack trace info. If I manually run the command, it looks like:

$ParseAddress Appname                       <- Run the command in console
Paste the addresses - new line to continue
[0]0xaaaaaaaa                               <- a User paste a list of address here
[1]0xbbbbbbbb
[2]0xcccccccc
                                            <- the user type 'enter' to continue
Stack Trace info:                           <-- results of running the ParseAddress
0xaaaaaaaa: Test::info::Runtest
0xbbbbbbbb: Test::info::SingleTestCase
0xcccccccc: ...

So, I thought the subprocess() and communicate() would be helpful in this case if I already has a list addresses in text format:

cmd = subprocess.Popen([ParseAddress, Appname], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
output, error = cmd.communicate(input="[0]0xaaaaaaaa\n[1]0xbbbbbbbb\n[2]0xcccccccc\n\n")
print(output)

The when I use the subprocess with communite(), the console output is:

$ParseAddress Appname
Paste the addresses - new line to continue
0xaaaaaaaa: ?? ??:0                         <- subprocess.PIPE?
0xbbbbbbbb: ?? ??:0
0xcccccccc: ?? ??:0

Stack Trace info: <-- no results from the ParseAddress since the address info are in incorrect format

If I use

cmd.communicate(input="[0]0xaaaaaaaa\r\n[1]0xbbbbbbbb\r\n[2]0xcccccccc\r\n\r\n")

The result is:

$ParseAddress Appname
Paste the addresses - new line to continue
                                           <-- No address here, no ideas why
Stack Trace info:

My question is why the parameters are changed from [0]0xaaaaaaaa to 0xaaaaaaaa: ?? ??:0?

What is the correct way to use communication()?

environment:
Ubuntu 18.04
Python 2.7.16 :: Anaconda, Inc.

r0n9
  • 2,505
  • 1
  • 29
  • 43
  • 1
    Describe what `ParseAddress` and `AppName` refer to. That's a black box that could be doing anything to the input you're providing. – Alex Reynolds Jun 10 '19 at 23:46
  • shouldn't you be providing `input` be a `bytes` object? – juanpa.arrivillaga Jun 10 '19 at 23:47
  • @juanpa.arrivillaga I tried `output, error = cmd.communicate(input=b"[0]0xaaaaaaaa\n[1]0xbbbbbbbb\n[2]0xcccccccc\n\n")` the result is the same – r0n9 Jun 11 '19 at 00:11
  • @AlexReynolds I updated my question, is it clear for now? Thanks – r0n9 Jun 11 '19 at 00:12
  • Are you reading text? try `universal_newlines=True` at `Popen` arguments, it will make it read the inputs as text instead of binary. – geckos Jun 11 '19 at 00:21
  • No, this isn't clear. I still have no idea what this command and its option(s) do. Hopefully someone else can help. – Alex Reynolds Jun 11 '19 at 00:21
  • @geckos, Yes, the input should be text and No, changing `universal_newlines` to `True` not fixing the problem. Thanks for your help – r0n9 Jun 11 '19 at 00:36
  • Are you running the process in the same directory and with the same environment as in the terminal? Also, you might be seeing some `stdout`/`stderr` mixing in that output, but that’s plainly not the data you *sent* or even a corruption of it. – Davis Herring Jun 11 '19 at 02:04

0 Answers0