Skip to content Skip to sidebar Skip to footer

How To Give Subprocess A Password And Get Stdout At The Same Time

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 ls , and if

Solution 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:
            breakifnot data:
            break
        result.append(data)
    pid, status = os.waitpid(pid, 0)
    returnstatus, ''.join(result)

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

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

Post a Comment for "How To Give Subprocess A Password And Get Stdout At The Same Time"