Search code examples
pythonsshpexpect

How to make pxssh expect other prompt in python


I am using pexpect to ssh to a device. Below is the code:

session = pxssh.pxssh()

if not session.login(ip, "username", "password"):
    print("SSH session failed on login")
    print(str(session))
    return False
else:
    print("SSH session login successfull")
    return True

I tested this locally on other device which has below prompt:

username@Machine54:~/

I then tried it on the server which has

username@server:/#

But it throws error:

Error could not synchronize with original prompt

How do I modify the code to ssh into server.


Solution

  • pxssh.login has an optional parameter original_prompt, which depending on your release is probably the pattern [#$], namely a hash or a dollar. This is described in the documentation as very optimistic and easily fooled. You should try setting one that is closer to your expected prompt, eg:

    if not session.login(ip, "username", "password",
                         original_prompt=r'@[^:]+:.*?/#?'):