Windows替代pexpect [英] Windows alternative to pexpect

查看:284
本文介绍了Windows替代pexpect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个跨平台的工具,该工具运行特定的命令,期望用于验证的某些输出,并发送用于验证的某些输出(例如用户名/密码)。

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.

在Unix上,我已经成功编写了使用 pexpect 库的Python工具(通过 pip install pexpect )。这段代码完美地工作了,而这正是我想要做的。我为下面的概念验证提供了一段代码摘录:

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))

我尝试在Windows上运行相同的源(将 / usr / bin / ctf 更改为 c:/ctf.exe ),我收到一条错误消息:

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'

根据 pexpect 文档


pexpect.spawn pexpect.run()在Windows上不可用,因为它们依赖Unix伪终端( ptys)。跨平台代码不得使用这些代码。

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

这使我开始寻找Windows等效语言。我已经尝试了流行的 winpexpect 项目此处,甚至是此处的最新版本(但分叉),但这些项目似乎都没有去工作。我使用的方法是:

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)

只能坐下来观看命令提示符什么也不做(似乎被困在 winspawn 方法中)。我想知道还有什么其他方法可以编写Python脚本来与命令行交互以达到与Unix相同的效果?如果不存在合适的Windows版本 pexpect 脚本,我还可以使用其他什么方法来解决这个问题?

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?

推荐答案

您可以使用 wexpect ( pexpect,Python软件基金会)。它具有相同的功能,并且可以在Windows上使用。

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

这篇关于Windows替代pexpect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆