2

I'm using the subprocess module to invoke plink and run some commands on a remote server. This works as expected, but after a successful call to subprocess.check_call or subprocess.check_output the raw_input method seems to block forever and doesn't accept input at the command line.

I've reduced it to this simple example:

import subprocess

def execute(command):
    return subprocess.check_call('plink.exe -ssh ' + USER + '@' + HOST + ' -pw ' + PASSWD + ' ' + command)

input = raw_input('Enter some text: ')
print('You entered: ' + input)

execute('echo "Hello, World"')

# I see the following prompt, but it's not accepting input
input = raw_input('Enter some more text: ')
print('You entered: ' + input)

I see the same results with subprocess.check_call and subprocess.check_output. If I replace the final raw_input call with a direct read from stdin (sys.stdin.read(10)) the program does accept input.

This is Python 2.7 on Windows 7 x64. Any ideas what I'm doing wrong?'

Edit: If I change execute to call something other than plink it seems to work okay.

def execute(command):
    return subprocess.check_call('cmd.exe /C ' + command)

This suggests that plink might be the problem. However, I can run multiple plink commands directly in a console window without issue.

zmb
  • 7,605
  • 4
  • 40
  • 55
  • I don't have a Windows machine around to test this, and can't replicate it here, but have you tried passing `close_fds=True` to the `subprocess.check_call` function? – Brett Lempereur Aug 26 '14 at 13:08
  • Just tried that and got the same result. – zmb Aug 26 '14 at 13:12
  • I can't reproduce this on my Windows machine. Does this happen if you run a program other than plink? I wonder if plink is messing with the terminal in some weird way that ends up breaking `raw_input`. – dano Aug 26 '14 at 16:26
  • Good idea. Tried that and it does seem to work correctly. Not sure what plink is doing. – zmb Aug 26 '14 at 16:47

1 Answers1

3

I was able to resolve this by attaching stdin to devnull:

def execute(command):
    return subprocess.check_call('plink.exe -ssh ' + USER + '@' + HOST + ' -pw ' + PASSWD + ' ' + command, stdin=open(os.devnull))
zmb
  • 7,605
  • 4
  • 40
  • 55