简体   繁体   English

如何使pxssh期望在python中出现其他提示

[英]How to make pxssh expect other prompt in python

I am using pexpect to ssh to a device. 我正在使用pexpect SSH到设备。 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. 如何修改代码以SSH进入服务器。

pxssh.login has an optional parameter original_prompt , which depending on your release is probably the pattern [#$] , namely a hash or a dollar. pxssh.login有一个可选参数original_prompt ,取决于您的版本,该参数可能是模式[#$] ,即哈希或美元。 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'@[^:]+:.*?/#?'):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM