Python - Pxssh - Getting an password refused error when trying to login to a remote server

StackOverflow https://stackoverflow.com/questions/15823011

  •  01-04-2022
  •  | 
  •  

Question

I'm trying to use the pexpect module pxssh to log in into my one of my server. I get password refused. I think I know what it is the problem, but can't figure out how to fix it. The issue is that there is a welcome banner when I log in into the server(Changing the banner is not an option) and the pexpect get confuse. Here is my code:

import pxssh

ssh = pxssh.pxssh()
ssh.login('192.168.1.10', 'username', 'password')

I'm expecting a 'password:', but pxssh is expecting original_prompt='[#$]'and those are symbols and in the banner the are couple of '.'

Any help, I will appreciate. Thanks

Was it helpful?

Solution

This error came for ssh is lock.so open the terminals and execute that command

xxxx@toad:~$ rm .ssh/known_hosts

remove the known_hosts

another options is you login in windows means check command prompt. if you try to login in windows means use for 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 uses the shell prompt to synchronize output from the remote host.

In order to make this more robust it sets the shell prompt to something more unique than just $ or #. This should work on most Borne/Bash or Csh style shells. Refer http://www.pythonforbeginners.com/code-snippets-source-code/ssh-connection-with-python/

OTHER TIPS

I don't know if you are right on your diagnostic, I don't think the banner can cause that but a wrong prompt can do it for sure. Look at the code to be sure. http://www.opensource.apple.com/source/lldb/lldb-69/test/pexpect-2.4/pxssh.py

Especially that part:

 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')

Disclaimer: this is not my code but Pxssh code. Why not using paramiko (http://www.lag.net/paramiko/) or pexpect directly?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top