Storing Subprocess Results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • topazcode

    Storing Subprocess Results

    I am using the subprocess module to run some shell commands on a Linux
    system:

    import subprocess
    output = subprocess.call ('''ssh server1 "uptime"''' , shell=True)

    The above assigns the output variable with a return code, i.e. 0 in
    this case. How can I actually capture the data returned from
    subprocess.call , rather than just the return code? I'd like to have
    the output variable contain the uptime string in this case. Any help
    is appreciated. Thanks.
  • Wojtek Walczak

    #2
    Re: Storing Subprocess Results

    On Tue, 2 Sep 2008 07:16:21 -0700 (PDT), topazcode wrote:
    I am using the subprocess module to run some shell commands on a Linux
    system:
    >
    import subprocess
    output = subprocess.call ('''ssh server1 "uptime"''' , shell=True)
    >
    The above assigns the output variable with a return code, i.e. 0 in
    this case. How can I actually capture the data returned from
    subprocess.call , rather than just the return code?
    Use subprocess.Pope n instead of call.

    --
    Regards,
    Wojtek Walczak,
    Cena domeny: 4999 PLN (do negocjacji). Możliwość kupna na raty od 624.88 PLN miesięcznie. Oferta sprzedaży znajduje się w serwisie Aftermarket.pl, największej giełdzie domen internetowych w Polsce.

    Comment

    • Karthik Gurusamy

      #3
      Re: Storing Subprocess Results

      On Sep 2, 7:16 am, topazcode <t...@topazcode .comwrote:
      I am using the subprocess module to run some shell commands on a Linux
      system:
      >
      import subprocess
      output = subprocess.call ('''ssh server1 "uptime"''' , shell=True)
      >
      The above assigns the output variable with a return code, i.e. 0 in
      this case.  How can I actually capture the data returned from
      subprocess.call , rather than just the return code?  I'd like to have
      the output variable contain the uptime string in this case.
      Probably commands module is a better choice for your problem:
      >>import commands
      >>commands.geto utput('fortune' )
      "While money can't buy happiness, it certainly lets you choose your own
      \nform of misery."
      >>>
      Karthik

       Any help
      is appreciated.  Thanks.

      Comment

      • topazcode

        #4
        Re: Storing Subprocess Results

        On Sep 2, 6:31 pm, Karthik Gurusamy <kar1...@gmail. comwrote:
        On Sep 2, 7:16 am, topazcode <t...@topazcode .comwrote:
        >
        I am using the subprocess module to run some shell commands on a Linux
        system:
        >
        import subprocess
        output = subprocess.call ('''ssh server1 "uptime"''' , shell=True)
        >
        The above assigns the output variable with a return code, i.e. 0 in
        this case.  How can I actually capture the data returned from
        subprocess.call , rather than just the return code?  I'd like to have
        the output variable contain the uptime string in this case.
        >
        Probably commands module is a better choice for your problem:>>impor t commands
        >commands.getou tput('fortune')
        >
        "While money can't buy happiness, it certainly lets you choose your own
        \nform of misery."
        >
        >
        >
        Karthik
        >
         Any help
        >
        is appreciated.  Thanks.
        Thanks guys. I went ahead and used subprocess.Pope n as suggested and
        that works fine. Did something like:

        import subprocess
        subprocess.Pope n("uptime", shell=True, stdout=subproce ss.PIPE,
        stderr=subproce ss.PIPE)
        stdout_value, stderr_value = subprocess.comm unicate()

        The above worked great. The 'uptime' was actually a fairly long
        stretch of commands, and this allows me to check for STDERR and act
        accordingly. Thanks again for the help and suggestions.

        Comment

        Working...