mz0
mz0

Reputation: 142

subprocess: stdout and stderror seem interchanged or mixed

i'm trying to automate interactive ssh calls (see note 1) as follows:

SSHBINARY = '/usr/bin/ssh'
ASKPASS   = '/home/mz0/checkHost/askpass.py'

def sshcmd(host,port,user,password,cmd):
    env0 = {'SSH_ASKPASS': ASKPASS, 'DISPLAY':':9999'}
    ssh = subprocess.Popen([SSHBINARY,"-T","-p %d" % port,
        "-oStrictHostKeyChecking=no", "-oUserKnownHostsFile=/dev/null", 
        "%s@%s" % (user,host), cmd],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    env=env0,
    preexec_fn=os.setsid
    )
    error = ssh.stderr.readlines()
    result = ssh.stdout.readlines()
    return error,result

host = 'localhost'
port = 22
user = 'try1'
password = '1try' # unused, hardcoded in ASKPASS
cmd = 'ls'

result1, error1 = sshcmd(host,port,user,password,cmd)
if result1 : print "OUT: %s" % result1
if error1  : print "ERR: %s" % error1

It turns i'm doing something stupid since i get this:

OUT: ["Warning: Permanently added 'localhost' (RSA) to the list of known hosts.\r\n"]
ERR: ['['Desktop\n', ..., 'Videos\n']']

Obviously stdout and stderr are swapped (note 2). Can you kindly point me my error?

note 1: i'm well aware of password-less ssh, dangers of ignoring host key etc. and hate requests on automating interactive ssh as much as you.

note 2: running the same command in shell confirms that stdout and stderr are swapped in my code

ssh -o 'UserKnownHostsFile /dev/null' -o 'StrictHostKeyChecking no' \
try1@localhost ls > ssh-out 2> ssh-error

Upvotes: 0

Views: 201

Answers (1)

Bernhard Barker
Bernhard Barker

Reputation: 55609

return error,result

and

result1, error1 = sshcmd(...)

Just swap either of those around.

Upvotes: 2

Related Questions