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.

2

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
@Shalomi11 yes the b indicates that the data being returned is bytes and not characters. See this for a more complete treatment: 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 stackoverflow.com/questions/36422572/… for a discussion
11

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

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

print out
Tue Oct  3 10:48:10 PDT 2017

Comments

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

Comments

-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
>>>

1 Comment

I feel like this doesn't answer the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.