11

I am trying to get the output of a command by doing ssh on a remote server using below command.

os.system('ssh user@host " ksh .profile; cd dir; find . -type f |wc -l"')

Output of this command is 14549 0

why is there a zero in the output ? is there any way of storing the output in variable or list ? I have tried assigning output to a variable and a list too but i am getting only 0 in the variable. I am using python 2.7.3.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jitu
  • 329
  • 1
  • 4
  • 16
  • 4
    If you're using Python 2.7, then use the `subprocess` module instead of `os.system`. – Fred Foo Oct 09 '13 at 09:12
  • possible duplicate of [How to save the data coming from "sudo dpkg -l" in Ubuntu terminal by using python](http://stackoverflow.com/q/14912104) and [How to save the data coming from "sudo dpkg -l" in Ubuntu terminal by using python](http://stackoverflow.com/q/13331684) – Martijn Pieters Oct 09 '13 at 09:17

4 Answers4

15

There are many good SO links on this one. try Running shell command from Python and capturing the output or Assign output of os.system to a variable and prevent it from being displayed on the screen for starters. In short

import subprocess
direct_output = subprocess.check_output('ls', shell=True) #could be anything here.

The shell=True flag should be used with caution:

From the docs: Warning

Invoking the system shell with shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.

See for much more info: http://docs.python.org/2/library/subprocess.html

Community
  • 1
  • 1
Paul
  • 7,155
  • 8
  • 41
  • 40
  • Hi my output has these characters: b'Fri Nov 27 14:20:49 CET 2020\n' . The b' and \n' . DO you know why this happen? @paul If i use os.system it does not come but I cant save it in a var – Shalomi90 Nov 27 '20 at 13:23
  • @Shalomi11 yes the b indicates that the data being returned is bytes and not characters. See this for a more complete treatment: https://docs.python.org/3/howto/unicode.html . In summary it will need to be decoded to return a string from the bytes (e.g. b'abc'.decode('utf8') ). The newline is just how the output is returned from the underlying command being used. See https://stackoverflow.com/questions/36422572/python-subprocess-output-without-n for a discussion – Paul Nov 30 '20 at 14:05
11

you can use os.popen().read()

import os
out = os.popen('date').read()

print out
Tue Oct  3 10:48:10 PDT 2017
stingray
  • 241
  • 2
  • 5
1

To add to Paul's answer (using subprocess.check_output):

I slightly rewrote it to work easier with commands that can throw errors (e.g. calling "git status" in a non-git directory will throw return code 128 and a CalledProcessError)

Here's my working Python 2.7 example:

import subprocess

class MyProcessHandler( object ):
    # *********** constructor
    def __init__( self ):
        # return code saving
        self.retcode = 0

    # ************ modified copy of subprocess.check_output()

    def check_output2( self, *popenargs, **kwargs ):
        # open process and get returns, remember return code
        pipe = subprocess.PIPE
        process = subprocess.Popen( stdout = pipe, stderr = pipe, *popenargs, **kwargs )
        output, unused_err = process.communicate( )
        retcode = process.poll( )
        self.retcode = retcode

        # return standard output or error output
        if retcode == 0:
            return output
        else:
            return unused_err

# call it like this
my_call = "git status"
mph = MyProcessHandler( )
out = mph.check_output2( my_call )
print "process returned code", mph.retcode
print "output:"
print out
Simon
  • 171
  • 1
  • 4
-3

If you are calling os.system() in an interactive shell, os.system() prints the standard output of the command ('14549', the wc -l output), and then the interpreter prints the result of the function call itself (0, a possibly unreliable exit code from the command). An example with a simpler command:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("echo X")
X
0
>>>
Lorenzo Gatti
  • 1,260
  • 1
  • 10
  • 15