简体   繁体   English

输入键命令有时在预期中不起作用

[英]Enter key command doesn't work sometimes in pexpect

I am running below pexpect script to login to an Avocent console server to connect to a network device.我在 pexpect 脚本下运行以登录到 Avocent 控制台服务器以连接到网络设备。 After entering the server password, it requires an 'Enter key' to be pressed for the prompt to appear.输入服务器密码后,需要按下“回车键”才能出现提示。 To achieve this, I tried child.sendline() , child.send('\n') and child.sendcontrol ('m') but none of these worked.为了实现这一点,我尝试了child.sendline()child.send('\n')child.sendcontrol ('m')但这些都不起作用。 I tried child.send('\r') , but it works intermittently.我试过child.send('\r') ,但它间歇性地工作。 Not sure what is causing the issue.不知道是什么导致了这个问题。

I saw that when the script gets stuck waiting for enter key, if I manually login to the console and send the enter key via keyboard, the pexpect script continues.我看到当脚本等待输入键时,如果我手动登录控制台并通过键盘发送输入键,pexpect 脚本会继续。

Here's my code snippet:这是我的代码片段:

child = pexpect.spawn('ssh local@x.x.x.x', timeout=120)
child.expect('Password:', timeout=60)
child.sendline(avocentpswd)
child.send('\r')
print "enter key sent"
cli = child.expect(['cisco#' , 'cisco>'])

Using pexpect==4.7.0 Python 2.7.5 OS: RHEL v7使用 pexpect==4.7.0 Python 2.7.5 操作系统:RHEL v7

Could someone please help.有人可以帮忙吗。

I checked the issues raised, but that didn't help: pexpect and sending an "Enter Key" issues我检查了提出的问题,但这没有帮助: pexpect 并发送“Enter Key”问题

You probably just need to wait for a second or two for the ssh to finish connecting and reset the tty mode to echo.您可能只需要等待一两秒钟让 ssh 完成连接并将 tty 模式重置为回显。 Try adding a import time;time.sleep(5) before sending the \r , and if it works use a loop something like (not tested):尝试在发送\r之前添加import time;time.sleep(5) ,如果它有效,请使用类似(未测试)的循环:

for tries in range(5):
   child.send('\r')
   cli = child.expect([pexpect.TIMEOUT, 'cisco#' , 'cisco>'], timeout=1)
   if cli!=0: break
else: ... fail ...
... ok ...

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

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