首页 > 解决方案 > 在 Windows 中使用 pexpect.popen_spawn.PopenSpawn() 运行 telnet 实例

问题描述

有没有人幸运地运行 pexpect.popen_spawn.PopenSpawn() 在 Windows 中运行 telnet?

请注意,我已经从 Windows 启用了“Telnet 客户端”功能,因为只需在命令行中键入“telnet”,我就可以从命令行很好地运行“telnet”。

import sys
import pexpect

from pexpect.popen_spawn import PopenSpawn

EXPECTED_PROMPT = "> "

def test():
    telnet_port=4444

    if sys.platform.startswith('win'):
        telnet = pexpect.popen_spawn.PopenSpawn(
            "C:\Windows\System32\telnet localhost {}".format(telnet_port),
            timeout=120,
            logfile=sys.stdout)
        telnet.expect(EXPECTED_PROMPT)

    return telnet

if __name__ == "__main__":
    telnet = test()

当我尝试运行上面的代码时,出现以下错误:

Traceback (most recent call last):
  File ".\test.py", line 26, in <module>
  telnet = test()
File ".\test.py", line 20, in test
  logfile=sys.stdout)
File "C:\Python27\lib\site-packages\pexpect\popen_spawn.py", line 53, in 
__init__
    self.proc = subprocess.Popen(cmd, **kwargs)
    File "C:\Python27\lib\subprocess.py", line 394, in __init__
    errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 644, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied

标签: pythonlinuxwindows

解决方案


通过使用 plink 解决了这个问题:https ://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

代替

C:\Windows\System32\telnet localhost {}

我用了

plink.exe -telnet localhost -P {}

推荐阅读