我正在pexpect脚本下运行,以登录Avocent控制台服务器以连接到网络设备。输入服务器密码后,需要按“ Enter键”以出现提示。为此,我尝试了child.sendline()
,child.send('\n')
和child.sendcontrol ('m')
,但这些都不起作用。我尝试过child.send('\r')
,但它会间歇性地工作。不知道是什么原因引起的。
[我看到脚本在等待输入键时卡住,如果我手动登录到控制台并通过键盘发送Enter键,则pexpect脚本会继续。
这是我的代码段:
child = pexpect.spawn('ssh [email protected]', timeout=120)
child.expect('Password:', timeout=60)
child.sendline(avocentpswd)
child.send('\r')
print "enter key sent"
cli = child.expect(['cisco#' , 'cisco>'])
使用pexpect == 4.7.0Python 2.7.5操作系统:RHEL v7
有人可以帮忙。
我检查了提出的问题,但这没有帮助:pexpect and sending an "Enter Key" issues
您可能只需要等待一两秒钟,以使ssh完成连接并将tty模式重置为echo。尝试在发送import time;time.sleep(5)
之前添加一个\r
,如果可行,请使用类似(未经测试)的循环: