subprocess.Popen("ssh host@remote cmd") failed
Question: I'm new to python subprocess. When I want to use python
subprocess.Popen.communicate to accomplish interact passwd with shell cmd "net
ads join -U administrator", it's output didn't redirect into PIPE, but in
stdout.My code is under:

    
    
    import subprocess
    
    import os
    import sys
    
    passwd = b'123456'
    cmd = "net ads join -U administrator"
    s1 = ("%s\n" % str(passwd)).encode('utf-8')
    p1 = subprocess.Popen(cmd1,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE,close_fds=True)
    out,err = p1.communicate(s1)
    print(out)
    

When I run the test, communicate didn't work, the output still in the stdout.

    
    
    $ python ads.py 
    Enter administrator's password:
    

When I change the cmd to "ssh root@ip ls /", I also met with the problem.

I tried pdb, but the problem seems not here. Any advise to deal with this
problem? Thank You


Answer: Here is a snippet class for SSH and run command remotely, that I used in one
of my scripts, please let me know if that works, Here I am using python
standard [PTY Module](http://docs.python.org/2/library/pty.html) for pseudo
terminal.

    
    
    import pty
    import os 
    import re
    
    class BaseError(Exception):
        pass
    
    class SSHError(BaseError):
        pass
    
    class Ssh:
    
        """SSH on the box"""
    
        def __init__(self, ip, passwd, user, port):
            self.ip = ip
            self.passwd = passwd
            self.user = user
            self.port = port
    
        def run_cmd(self, c):
            (pid, f) = pty.fork()
            if pid == 0:
                os.execlp(
                    "ssh", "ssh", '-o ConnectTimeout=10',
                    '-o StrictHostKeyChecking=no',
                    '-o UserKnownHostsFile=/dev/null', '-q',
                    '-p %d' % self.port,
                    self.user + '@' + self.ip, c)
            else:
                return (pid, f)
    
        def run_scp(self, src, dst):
            (pid, f) = pty.fork()
            if pid == 0:
                os.execlp(
                    "scp", "scp", '-o StrictHostKeyChecking=no',
                    '-o UserKnownHostsFile=/dev/null', '-q',
                    '-P %d' % self.port, "-r", src,
                    self.user + '@' + self.ip + ':' + dst)
            else:
                return (pid, f)
    
        def read(self, f):
            x = ""
            try:
                x = os.read(f, 2024)
            except Exception:
                pass
            return x
    
        def sshOutputs(self, pid, f):
    
            output = ""
            readOutLoud = self.read(f)
            m = re.search("authenticity of host", readOutLoud)
    
            if m:
                os.write(f, 'yes\n')
                while True:
                    readOutLoud = self.read(f)
                    m = re.search("Permanently added", readOutLoud)
                    if m:
                        break
    
                readOutLoud = self.read(f)
            m = re.search("assword:", readOutLoud)
    
            if m:
                os.write(f, self.passwd + '\n')
                tmp = self.read(f)
                tmp += self.read(f)
                m = re.search("Permission denied", tmp)
                if m:
                    raise SSHError("Invalid passwd")
                p = re.search("[pP]assword", tmp)
                if p:
                    raise SSHError("Invalid passwd")
    
                # passwd was accepted
                readOutLoud = tmp
    
            while readOutLoud and len(readOutLoud) > 0:
                output += readOutLoud
                readOutLoud = self.read(f)
    
            os.waitpid(pid, 0)
            os.close(f)
    
            return output
    
        def exe_ssh_cmd(self, c):
            (pid, f) = self.run_cmd(c)
            return self.sshOutputs(pid, f)
    
        def exe_scp_cmd(self, src, dst):
            (pid, f) = self.run_scp(src, dst)
            return self.sshOutputs(pid, f)
    

