Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 输入键命令不';I don’我有时不工作_Python 2.7_Ssh_Enter_Pexpect_Pxssh - Fatal编程技术网

Python 2.7 输入键命令不';I don’我有时不工作

Python 2.7 输入键命令不';I don’我有时不工作,python-2.7,ssh,enter,pexpect,pxssh,Python 2.7,Ssh,Enter,Pexpect,Pxssh,我正在运行pexpect脚本,以登录到Avocent控制台服务器,连接到网络设备。输入服务器密码后,需要按“回车键”才能出现提示。为了实现这一点,我尝试了child.sendline(),child.send('\n')和child.sendcontrol('m'),但这些都不起作用。 我尝试了child.send('\r'),但它间歇性工作。不确定是什么导致了问题 我看到,当脚本在等待enter键时被卡住,如果我手动登录到控制台并通过键盘发送enter键,pexpect脚本将继续 以下是我的代

我正在运行pexpect脚本,以登录到Avocent控制台服务器,连接到网络设备。输入服务器密码后,需要按“回车键”才能出现提示。为了实现这一点,我尝试了
child.sendline()
child.send('\n')
child.sendcontrol('m')
,但这些都不起作用。 我尝试了
child.send('\r')
,但它间歇性工作。不确定是什么导致了问题

我看到,当脚本在等待enter键时被卡住,如果我手动登录到控制台并通过键盘发送enter键,pexpect脚本将继续

以下是我的代码片段:

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>'])
使用pexpect==4.7.0 Python 2.7.5 操作系统:RHELV7

有人能帮忙吗

我检查了提出的问题,但没有帮助:

您可能只需要等待一两秒钟,ssh就可以完成连接并将tty模式重置为echo。尝试添加
导入时间;time.sleep(5)
在发送
\r
之前,如果它工作,请使用类似(未测试)的循环:

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