期望PopenSpawn在Windows上不起作用

问题描述 投票:1回答:1

我目前正在尝试使用pexpect控制到我的树莓派的简单ssh连接,我将其用于某些智能家居控件。

我刚刚开始使用它,我正在从一个学期的书“ Violent Python”的帮助下学习它。

我在Windows上,并了解到pexpect.spawn对象在Windows上不可用。在文档中,有人说我必须改用PopenSpawn。我有一个简单的脚本:

import traceback
import pexpect
from pexpect.popen_spawn import PopenSpawn

try:
    child = pexpect.popen_spawn.PopenSpawn('ssh [email protected]')
    a = child.expect(['password:', 'The authenticity of host'], timeout=300, async=True)
    if a == 0:
        child.sendline('123456789')
        print('this ip has exists in know_host files!')
    if a == 1:
        print('this ip will be added to know_host files!')
        child.sendline('yes')
        child.expect('password:')
        child.sendline('raspberry')
        # send test
        child.sendline("echo TestMessage")
        child.interact()
except pexpect.EOF:
    traceback.print_exc()

这总是会给我这个错误:

Traceback (most recent call last):
  File "D:/CENSORED/pexpect-master/sshforpi2.py", line 6, in <module>
    child = pexpect.popen_spawn.PopenSpawn('ssh [email protected]')
  File "D:/CENSORED/pexpect-master\pexpect\popen_spawn.py", line 46, in __init__
    self.proc = subprocess.Popen(cmd, **kwargs)
  File "C:\Python34\lib\subprocess.py", line 859, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1112, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system can't find the given file.

[我发现this teddyjoe个人在问同样的事情-他还没有得到答案。

python pexpect
1个回答
0
投票

SSH不是Windows命令的本机。在下面的线路图命令中,指向C:\ Windows \ System32 \ OpenSSH中的ssh。现在,OpenSSH已添加到System32中的Windows 10中。试试这个,我认为它可能对您有用。

child = pexpect.popen_spawn.PopenSpawn('ssh [email protected]')

尝试此操作

command = 'C:\Windows\system32\OpenSSH\ssh.exe [email protected]'
child = pexpect.popen_spawn.PopenSpawn(command)
© www.soinside.com 2019 - 2024. All rights reserved.