简体   繁体   English

Pexpect:无法设置 shell 提示

[英]Pexpect: Could not set shell prompt

I have referred a python script from https://pexpect.readthedocs.io/en/stable/api/pxssh.html in order to perform a ssh password based login.我从https://pexpect.readthedocs.io/en/stable/api/pxssh.html引用了一个 python 脚本,以便执行基于 ssh 密码的登录。 Code is as follows:代码如下:

from pexpect import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = input('hostname: ')
    username = input('username: ')
    password = getpass.getpass('password: ')
    s.login(hostname, username, password)
    s.sendline('uptime')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.sendline('ls -l')
    s.prompt()
    print(s.before)
    s.sendline('df')
    s.prompt()
    print(s.before)
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")
    print(e)

When i run the code, it prompts for hostname, username and password.当我运行代码时,它会提示输入主机名、用户名和密码。 After these inputs are provided through CLI,the ssh login fails with the exception as follows:通过 CLI 提供这些输入后,ssh 登录失败,异常如下:

could not set shell prompt (received: b"unset PROMPT_COMMAND\r\nPS1='[PEXPECT]\\$ '\r\nUser1@167.254.225.14's password: \r\nUser1@167.254.225.14's password: ", expected: '\\[PEXPECT\\][\\$\\#] ').

The manual login works fine for the same ssh credentials.手动登录适用于相同的 ssh 凭据。

Note:
Python version: 3.6
Pexpect version: 4.7 

The error was due to login_timeout which is by default 10 sec.该错误是由于 login_timeout 造成的,默认情况下为 10 秒。 Setting high login_timeout in login() solved the problem.在 login() 中设置高 login_timeout 解决了这个问题。

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

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