Search code examples
pythonpexpectpxssh

Python pexpect.pxssh.ExceptionPxssh: password refused


The following credentials are correct.

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

However it didn't work with 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
>>> 

Solution

  • Your error stems from the positional arguments that you're giving the method pexpect.pxssh.pxssh.login.

    According to the documentation, the first four positional arguments of the method is:

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

    So when you're calling s.login(hostname, username, password, port) you're setting the terminal_type to the port-number.

    If you change the last argument into a keyword-argument instead, the port will be set correctly:

    s.login(hostname, username, password, port=port)