简体   繁体   English

Pexpect在“是/否”提示下不起作用

[英]Pexpect not working at Yes/No prompt

I'm using pexpect to login and run commands on a cisco router. 我正在使用pexpect在Cisco路由器上登录并运行命令。 It's getting stuck at the RSA yes/no prompt and not sending "yes". 它卡在RSA是/否提示下,并且不发送“是”。 Here is a snippet of the code: 这是代码片段:

fout = file(filepath,'w')
child = pexpect.spawn('ssh %s@%s' % (username, agg))
child.logfile_read = fout
rsa_key = '\(yes\/no\)\?' 
prompt = "Password:"
i = child.expect([rsa_key,prompt,""])
if i==0:
    child.sendline('yes')
    child.expect(prompt)
    child.sendline(password)
elif i==1:
    child.sendline(password)
else:       
    child.sendline(password)

child.expect('>')
child.sendline('enable')
child.expect('Password:')
child.sendline(password)
child.expect('#')
child.sendline('terminal length 0')
child.expect('#')
child.sendline('show processes cpu history')
child.expect('#')
child.sendline('logout')

Here's the error I get: 这是我得到的错误:

Traceback (most recent call last):
  File "/usr/pre_check.py", line 378, in <module>
    agg_check = agg_checks(agg,username,password)
  File "/usr/pre_check.py", line 198, in agg_checks
    child.expect('>')
  File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 327, in expect
    timeout, searchwindowsize, async_)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 355, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 104, in expect_loop
    return self.timeout(e)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 68, in timeout
    raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7f6c5e4b89d0>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'username@router']
buffer (last 100 chars): 'TyefPiveH4nHv1dfvbL/MFbqSR+dup1U7Cn+JaDeq0.\r\nAre you sure you want to continue connecting (yes/no)? '
before (last 100 chars): 'TyefPiveH4nHv1dfvbL/MFbqSR+dup1U7Cn+JaDeq0.\r\nAre you sure you want to continue connecting (yes/no)? '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 28991
child_fd: 7
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: <open file 'mylogfile.txt', mode 'w' at 0x7f6c5e53ee40>
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile(">")

I have tried varius versions of the rsa_key variable with the same problem: 我已经尝试过rsa_key变量的varius版本,并且存在相同的问题:

  • Are you sure you want to continue connecting (yes/no)\\? 您确定要继续连接(是/否)\\吗?
  • Are you sure you want to continue connecting (yes/no)? 您确定要继续连接(是/否)吗?
  • Are you sure you want to continue connecting 您确定要继续连接吗
  • (yes/no)? (是/否)?
  • (yes/no)\\? (是/否)\\?
  • (yes/no) (是/否)

Here is what it looks like when ssh'ing to the switch: 这是在切换到交换机时的样子:

ssh username@routername
The authenticity of host 'routername (11.111.111.1)' can't be established.
RSA key fingerprint is SHA256:KCS1HKzltOwHgg+WhTsV3OTxNzxUQwN9T3Jp1lGVu00.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'routername,11.111.111.1' (RSA) to the list of known hosts.
Password: 

routername>

Personally I would dodge that whole bit with ssh -o StrictHostKeyChecking=no since it seems you don't care to make a decision based off of any interpretation. 就个人而言,我会用ssh -o StrictHostKeyChecking=no来回避整个问题,因为似乎您不在乎根据任何解释做出决定。 I'm not sure your use case for the empty string match in the list being passed to expect call, however it seems that regardless of the index, you are sending the password and expecting the prompt as if initial authentication and hostchecking is completed. 我不确定要传递给列表的空字符串匹配的用例以进行调用,但是似乎无论索引如何,您都在发送密码并期望出现提示,就像初始身份验证和主机检查已完成一样。 The error you received shows the buffer as containing the yes/no prompt but expecting failing while expecting a > . 您收到的错误将缓冲区显示为包含是/否提示,但在期望>同时期望失败。 It is possible that your password is being sent to yes no prompt and then presenting "Please type 'yes' or 'no':" which doesn't contain > causing you to time out. 您的密码可能会发送到“是”,“否”提示,然后显示不包含“ > ”的“请输入'是'或'否':”,这会使您超时。

You need to use '(?i)yes/no'. 您需要使用“(?i)是/否”。 I ran into this same issue. 我遇到了同样的问题。

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

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