Windows alternative to pexpect

16,502

You can use wexpect ("Windows alternative of pexpect", Python Software Foundation). It has the same functions, and it works on Windows .

Share:
16,502

Related videos on Youtube

Dylan Wheeler
Author by

Dylan Wheeler

I grew up in the small town of Bow, NH, and graduated summa cum laude from the University of New Hampshire with bachelor's degrees in information technology and philosophy. I'm an entrepreneur at heart and founded two EdTech startups. ecoText's mission is to become the ultimate collaborative learning tool while delivering affordable digital textbooks to students. I helped the company raise over $650,000 in funding. In high school, I founded Triumph Software to launch a professional development management tool, Loggit. The app has since served over 500 educators across New Hampshire and tracked more than 100,000 professional development hours. When I'm not tackling challenging problems, I enjoy writing, photography, and building deep interpersonal relationships while pondering my place in the universe.

Updated on June 04, 2022

Comments

  • Dylan Wheeler
    Dylan Wheeler 7 months

    I'm trying to write a cross-platform tool that runs specific commands, expects certain output for verification, and sends certain output (like username/password) for authentication.

    On Unix, I have been successful in programming a Python tool that uses the pexpect library (via pip install pexpect). This code works perfectly and is exactly what I am trying to do. I've provided a small excerpt of my code for proof-of-concept below:

    self.process = pexpect.spawn('/usr/bin/ctf', env={'HOME':expanduser('~')}, timeout=5)
    self.process.expect(self.PROMPT)
    self.process.sendline('connect to %s' % server)
    sw = self.process.expect(['ERROR', 'Username:', 'Connected to (.*) as (.*)'])
    if sw == 0:
        pass
    elif sw == 1:
        asked_for_pw = self.process.expect([pexpect.TIMEOUT, 'Password:'])
        if not asked_for_pw:
            self.process.sendline(user)
            self.process.expect('Password:')
        self.process.sendline(passwd)
        success = self.process.expect(['Password:', self.PROMPT])
        if not success:
            self.process.close()
            raise CTFError('Invalid password')
    elif sw == 2:
        self.server = self.process.match.groups()[0]
        self.user = self.process.match.groups()[1].strip()
    else:
        info('Could not match any strings, trying to get server and user')
        self.server = self.process.match.groups()[0]
        self.user = self.process.match.groups()[1].strip()
    info('Connected to %s as %s' % (self.server, self.user))
    

    I tried running the same source on Windows (changing /usr/bin/ctf to c:/ctf.exe) and I receive an error message:

    Traceback (most recent call last):
      File ".git/hooks/commit-msg", line 49, in <module> with pyctf.CTFClient() as c:
      File "C:\git-hooktest\.git\hooks\pyctf.py", line 49, in __init__
        self.process = pexpect.spawn('c:/ctf.exe', env={'HOME':expanduser('~')}, timeout=5)
      AttributeError: 'module' object has no attribute 'spawn'
    

    According to the pexpect documentation:

    pexpect.spawn and pexpect.run() are not available on Windows, as they rely on Unix pseudoterminals (ptys). Cross platform code must not use these.

    That led me on my search for a Windows equivalent. I have tried the popular winpexpect project here and even a more recent (forked) version here, but neither of these projects seem to work. I use the method:

    self.process = winpexpect.winspawn('c:/ctf.exe', env={'HOME':expanduser('~')}, timeout=5)
    

    only to sit and watch the Command Prompt do nothing (it seems as though it's trapped inside the winspawn method). I was wondering what other means I could go about programming a Python script to interact with the command line to achieve the same effect as I have been able to in Unix? If a suitable working Windows-version pexpect script does not exist, what other means could I use to go about this?

  • Mario Román
    Mario Román over 4 years
    Please note that PopenSpawn is not a direct replacement for spawn, and this solution may not work with interactive terminal programs.
  • ChrisCantrell
    ChrisCantrell over 4 years
    I had to do an extra import to get this working. See github.com/pexpect/pexpect/issues/328
  • Sylvaus
    Sylvaus about 2 years
    Could you provide an example of DockerFile or docker-compose.yml showing how someone can encapsulate a Python script in docker? Also could you mention what are required for docker to be able to run Linux container in Windows to provide a better, more complete answer to the question ?
  • katheravan arumugam
    katheravan arumugam about 2 years
    The Example for Encapsulating Python in container: In Linux : Step 1: docs.docker.com/engine/install/centos After the installation ,one can get docker as a sytem command in linux. Step 2 : Create a directory , and copy the python in that directory and create a ne file called "Dockerfile" and also have the requirements.txt