我目前正在尝试使用pexpect控制与我的覆盆子pi的简单ssh连接,我将其用于某些智能家居控件。
我刚刚开始使用它,我是在“暴力Python”的帮助下学习的,这是一本penetester的书。
我在Windows上并了解到pexpect.spawn
对象在Windows上不可用。在文档中,据说我必须使用PopenSpawn
代替。我有一个简单的脚本:
import traceback
import pexpect
from pexpect.popen_spawn import PopenSpawn
try:
child = pexpect.popen_spawn.PopenSpawn('ssh pi@192.168.178.34')
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 pi@192.168.178.34')
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家伙问同样的事情 - 他还没有得到答案。
答案 0 :(得分:0)
SSH不是Windows命令的本机。在下面的线路图命令中,指向C:\ Windows \ System32 \ OpenSSH中的ssh。现在,OpenSSH已添加到System32中的Windows 10中。试试这个,我认为它可能对您有用。
child = pexpect.popen_spawn.PopenSpawn('ssh pi@192.168.178.34')
改为尝试
command = 'C:\Windows\system32\OpenSSH\ssh.exe pi@192.168.178.34'
child = pexpect.popen_spawn.PopenSpawn(command)
更好的是,您可以安装适用于Linux的新Windows子系统并以此运行脚本。 Windows Subsystem for Linux Installation Guide for Windows 10