subprocess.Popen and replacing the shell pipe line

T

Tom Brown

I need to chain together three linux commands and get the final output. I read
the documentation for Popen in the subprocess module for replacing the shell
pipe line. I followed the example and keep getting a 0 where I should be
getting a 1.

I am trying to do this:
grep "Sep 22" /var/log/auth.log | grep "Illegal user" | wc -l
which returns a 1 when I run this manually.

This is what I did with python:

p1 = Popen(['grep', '"Sep 22"', '/var/log/auth.log'], stdout=PIPE)
p2 = Popen(['grep', '"Illegal user"'], stdin=p1.stdout, stdout=PIPE)
p3 = Popen(['wc', '-l'], stdin=p2.stdout, stdout=PIPE)
print p3.stdout.read()

which always prints a 0. What am I doing wrong?

Thanks,
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top