我正在尝试使用pexpect模块pxssh登录我的服务器。我的密码被拒绝了。我想我知道这是什么问题,但无法弄清楚如何解决它。问题是当我登录服务器时有一个欢迎横幅(更改横幅不是一个选项)并且pexpect变得混乱。这是我的代码:
import pxssh
ssh = pxssh.pxssh()
ssh.login('192.168.1.10', 'username', 'password')
我期待'密码:',但是pxssh期待original_prompt ='[#$]'并且这些是符号,并且在横幅中有几个'。'
任何帮助,我将不胜感激。感谢
答案 0 :(得分:2)
这个错误来自ssh是lock.so打开终端并执行该命令
xxxx@toad:~$ rm .ssh/known_hosts
删除known_hosts
另一个选项是您在Windows中登录意味着检查命令提示符。 如果您尝试登录Windows,则意味着使用pexpect
child = pexpect.spawn('ssh tiger@172.16.0.190 -p 8888')
child.logfile = open("/tmp/mylog", "w")
print child.before
child.expect('.*Are you sure you want to continue connecting (yes/no)?')
child.sendline("yes")
child.expect(".*assword:")
child.sendline("tiger\r")
child.expect('Press any key to continue...')
child.send('\r')
child.expect('C:\Users\.*>')
child.sendline('dir')
child.prompt('C:\Users\.*>')
pxssh使用shell提示符来同步远程主机的输出。
为了使其更加健壮,它将shell提示设置为更多内容 不仅仅是$或#。这应该适用于大多数Borne / Bash或Csh风格 炮弹。 的参见强> http://www.pythonforbeginners.com/code-snippets-source-code/ssh-connection-with-python/
答案 1 :(得分:0)
我不知道你的诊断是否正确,我不认为横幅可以导致这种情况,但错误的提示可以肯定地做到。看看代码是否确定。 http://www.opensource.apple.com/source/lldb/lldb-69/test/pexpect-2.4/pxssh.py
特别是那部分:
elif i==2: # password prompt again
# For incorrect passwords, some ssh servers will
# ask for the password again, others return 'denied' right away.
# If we get the password prompt again then this means
# we didn't get the password right the first time.
self.close()
raise ExceptionPxssh ('password refused')
免责声明:这不是我的代码,而是 Pxssh 代码。 为什么不直接使用 paramiko (http://www.lag.net/paramiko/)或 pexpect ?