1

I want to invoke an exe on windows with python. The exe file so invoked processes something internally and then prompts for an input and once this is entered prompts for another input. Therefore, I want to keep the prompt inputs in a python list and then invoke the exe. Wait for the prompt to appear and then provide the first string in list and then provide the second string in list on second prompt. Basically I want to create a function in python that is able to act like expect on windows.

Tried the below code provided here: Interact with a Windows console application via Python, but this doesn't seem to work anymore with windows 10:

from subprocess import *
import re

class InteractiveCommand:
    def __init__(self, process, prompt):
        self.process = process
        self.prompt  = prompt
        self.output  = ""
        self.wait_for_prompt()

    def wait_for_prompt(self):
        while not self.prompt.search(self.output):
            c = self.process.stdout.read(1)
            if c == "":
                break
            self.output += c

        # Now we're at a prompt; clear the output buffer and return its contents
        tmp = self.output
        self.output = ""
        return tmp

    def command(self, command):
        self.process.stdin.write(command + "\n")
        return self.wait_for_prompt()

p      = Popen( ["cmd.exe"], stdin=PIPE, stdout=PIPE )
prompt = re.compile(r"^C:\\.*>", re.M)
cmd    = InteractiveCommand(p, prompt)

listing = cmd.command("dir")
cmd.command("exit")

print(listing)

Could someone please help?

codecrazy46
  • 275
  • 1
  • 5
  • 14
  • The program that you're running (vpnclient.exe) will likely buffer its output when writing to a pipe. There's no common way around that in Windows. It may have a command-line option to force no buffering or line buffering. Otherwise you'll have to get creative. – Eryk Sun Oct 04 '20 at 02:15
  • If you're only running on updated installations of Windows 10, you could create a pseudoconsole session (ConPTY) that lets you directly read from and write to the child's console session. But ConPTY is relatively new, and Python's standard library doesn't have any support for it yet. It would require extensive use of ctypes, CFFI, or a C extension module. – Eryk Sun Oct 04 '20 at 02:17
  • Use wexpect package, install it with pip (https://wexpect.readthedocs.io/en/latest/) – Francesco Jul 20 '21 at 06:55

1 Answers1

0

Subprocess package works fine with Windows 10. Try the following commands.

import subprocess

p = subprocess.Popen('dir', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = p.communicate()
print(out.decode('utf-8'))

Update Your code runs fine in Python 2.7. The problem seems to be with Python 3.

enter image description here

Fadi Abu Raid
  • 831
  • 5
  • 13
  • I am invoking an exe instead of "dir". e.g. vpnclient.exe. Once this is invoked, it prompts for the username and password. I need a way to wait for the prompts to appear and pass the respective inputs. It is also better if we could stream the output, or at least once the connection is established the script should exit. – codecrazy46 Oct 03 '20 at 17:55