how to give subprocess a password and get stdout a

2019-02-20 09:29发布

问题:

I'm trying to check for the existence of an executable on a remote machine, then run said executable. To do so I'm using subprocess to run ssh <host> ls <file>, and if that's successful, run ssh <host> <file>. ssh asks for a password, of course, and I'd like to provide that automatically. Also, I'd like to get the returncode from ls, and stdout and stderr from running the command.

So I know the communicate() method is needed, to avoid deadlocks, but I can't get the password to be recognized by Popen(stdin). Also I'm using Python 2.4.3, and stuck on that version. Here's the code I've got so far:


import os  
import subprocess as sb  

def WallHost(args):  
    #passwd = getpass.getpass()  
    passwd = "password"  
    for host in args:   

        # ssh to the machine and verify that the script is in /usr/bin   
        sshLsResult = sb.Popen(["ssh", host, "ls", "/usr/bin/wall"], stdin=sb.PIPE, stderr=sb.PIPE, stdout=sb.PIPE)  
        (sshLsStdout, sshLsStderr) = sshLsResult.communicate(input=passwd)  
        sshResult = sshLsResult.returncode  

        if sshResult != 0:                      
            raise "wall is not installed on %s. Please check." % host  
        else:  
            sshWallResult = sb.Popen(["ssh", host, "/usr/bin/wall", "hello world"], stdin=sb.PIPE, stderr=sb.PIPE, stdout=sb.PIPE)  
            (sshWallStdout, sshWallStderr) = sshWallResult.communicate(input=passwd)  
            print "sshStdout for wall is \n%s\nsshStderr is \n\n" % (sshWallStdout, sshWallStderr)  

args = ["127.0.0.1", "192.168.0.1", "10.10.265.1"]  
WallHost(args)  

Any help getting the process to accept that password is appreciated. Or if you've got a better way to check for the executable and then run it on a remote host. ;)

thx anthony

回答1:

How about using authorized_keys. Then, you don't need to input password.

You can also go hard way (only work in Linux):

import os
import pty

def wall(host, pw):
    pid, fd = pty.fork()
    if pid == 0: # Child
        os.execvp('ssh', ['ssh', host, 'ls', '/usr/bin/wall'])
        os._exit(1) # fail to execv

    # read '..... password:', write password
    os.read(fd, 1024)
    os.write(fd, pw + '\n')

    result = []
    while True:
        try:
            data = os.read(fd, 1024)
        except OSError:
            break
        if not data:
            break
        result.append(data)
    pid, status = os.waitpid(pid, 0)
    return status, ''.join(result)

status, output = wall('localhost', "secret")
print status
print output

http://docs.python.org/2/library/pty.html