Python - ssh with password to foreign computer without using non-built-in modules
Question: Before you down vote this, let me say that I've read ALOT of questions on this
subject on SO and haven't found the answer. This one has the closest thing to
what I need though on the answer by "Neil".

[What is the simplest way to SSH using
Python?](http://stackoverflow.com/questions/1233655/what-is-the-simplest-way-
to-ssh-using-python)

I would like to find a way to log into another host using ssh, providing a
password, without importing non-standard python modules. I would then need to
be able to send multiple commands using the ssh connection. I need to do this
because I need to automate something on a lot of closed systems that I cannot
get new modules on to (non internet accessible).

The answer in the referenced question is:

> If you want to avoid any extra modules, you can use the subprocess module to
> run
>
> ssh [host] [command] and capture the output.
>
> Try something like:
>
> process = subprocess.Popen("ssh example.com ls", shell=True,
> stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output,stderr =
> process.communicate() status = process.poll() print output To deal with
> usernames and passwords, you can use subprocess to interact with the ssh
> process, or you could install a public key on the server to avoid the
> password prompt.

Which sounds good, but I can't quite understand this well enough to take it
farther. Can someone help me to get a baby step going?

    
    
    (1) log in to ssh
    (2) provide the password when its prompted
    (3) issue an ls command and display the result
    

I can do the rest of the automation if I can just get pass password entry.

PS: I tried using expect, but TCL isn't on the system so please don't tell me
to use that! If you know something else I can call to aid python I'm happy to
try it though as long as its likely to be built into a Fedora Enterprise Linux
system by default.


Answer: This code uses Popen.communicate() to run a command. As described in chapter
17 of the Python Library Reference, communicate() opens pipes for stdin,
stdout, and stderr, starts up the subprocess, and waits for the process to
terminate. FYI for other readers: startupinfo is the Windows-specific way of
running the subprocess under a GUI app without popping up a black console
window; it's not needed when running as a console script or under Linux.

    
    
    def runcmd(self, prog, args, inputs):
        cmd = " ".join((prog, args))
        si = subprocess.STARTUPINFO()
        si.dwFlags = subprocess.STARTF_USESHOWWINDOW
        process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, startupinfo=si)
        return(process.communicate(input=inputs))
    

It can be called with:

    
    
        p = self.runcmd("c:\Program Files\ApplicationDir\application.exe", "arguments", "inputs")
        result = str(p[0],"ascii").strip()
        print(result)
    

The result a 2-tuple (stdout, stderr), which should be converted from byte
strings to strings. The password passed as "inputs" should be a byte string.

