Python pexpect.pxssh.ExceptionPxssh:密码被拒绝

问题描述

以下凭据正确。

wolf@linux:~$ sshpass -p bandit0 ssh [email protected] -p 2220
This is a OverTheWire game server.

但是它不适用于Python pexpect.pxssh

>>> from pexpect import pxssh
>>> s = pxssh.pxssh()
>>> hostname = 'bandit.labs.overthewire.org'
>>> username = 'bandit0'
>>> password = 'bandit0'
>>> port = '2220'
>>> s.login(hostname,username,password,port)
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/usr/lib/python3/dist-packages/pexpect/pxssh.py",line 402,in login
    raise ExceptionPxssh('password refused')
pexpect.pxssh.ExceptionPxssh: password refused
>>> 

解决方法

您的错误源于您为方法pexpect.pxssh.pxssh.login提供的位置参数。

根据文档,该方法的前四个位置参数为:

server,username=None,password='',terminal_type='ansi'

因此,当您呼叫s.login(hostname,username,password,port)时,就是将terminal_type设置为端口号。

如果您将最后一个参数改为关键字参数,则将正确设置端口:

s.login(hostname,port=port)