subprocess.Popen and replacing the shell pipe line

Discussion in 'Python' started by Tom Brown, Sep 22, 2005.

  1. Tom Brown

    Tom Brown Guest

    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
    Tom Brown, Sep 22, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Walzer
    Replies:
    1
    Views:
    373
    Martin P. Hellwig
    Dec 12, 2008
  2. Andrew Parker

    subprocess.Popen not creating a pipe

    Andrew Parker, Feb 1, 2009, in forum: Python
    Replies:
    0
    Views:
    246
    Andrew Parker
    Feb 1, 2009
  3. Helmut Jarausch
    Replies:
    8
    Views:
    2,230
  4. Steven K. Wong
    Replies:
    6
    Views:
    1,009
    Nobody
    Jan 10, 2010
  5. goodman
    Replies:
    4
    Views:
    628
    Lawrence D'Oliveiro
    Nov 5, 2010
Loading...

Share This Page