subprocess.Popen and replacing the shell pipe line

Tom Brown brown at esteem.com
Thu Sep 22 18:38:21 EDT 2005


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



More information about the Python-list mailing list