[Tutor] how to control putty window

eryksun eryksun at gmail.com
Fri Dec 21 18:12:03 CET 2012


On Fri, Dec 21, 2012 at 9:44 AM, Ufuk Eskici <ufukeskici at gmail.com> wrote:
>
> cmd = "plink -ssh -l ufuk10.10.10.10 -pw password"
> process = subprocess.Popen(cmd)
> inputdata="r van"
> result = process.communicate(inputdata)
>
> But after the successful SSH, I cannot continue, no command runs:


To use communicate(), you need to set one or more of the standard
streams to a file or pipe (e.g. stdout=subprocess.PIPE). That said, if
you just have a single command, it's simpler to have the ssh client
execute it. Get the result using check_output (it sets up and calls
communicate):

    user = 'ufuk'
    password = 'password'
    host = "10.10.10.10"
    remote_cmd = 'r van'
    cmd = ['plink', '-ssh', '-l', user, '-pw', password, '"%s"' % remote_cmd]
    result = subprocess.check_output(cmd, stdin=subprocess.PIPE)

I had to add stdin=subprocess.PIPE when trying this interactively.
Otherwise plink leaves the console stdin in an unusable state. This is
probably the source of the lockup you're getting.

If you need an interactive, stateful session, then communicate() won't
help since it closes the streams. You'll have to roll your own by
manually handling the stdin/stdout pipes. That means you'll need a
background thread to get around readline blocking (select only works
for sockets on Windows). You'll hit a brick wall with this approach if
the program uses full buffering in a pipe. With Linux you can
sometimes tweak the streams using stdbuf, but not if the program uses
setvbuf(). To get around this in Unix you can use pexpect to fake a
tty. I think that's only available on Windows via Cygwin.


More information about the Tutor mailing list