下面的类被设计用来操作类似cisco的设备接口,以执行命令和更新配置元素。在
按照目前的情况,我可以实例化该类,调用ssh_to_aos_expsh
函数并返回有效输出(例如,当命令为“show running config”时获取配置)。但是,当我调用ssh_to_aos_config
函数(它调用ssh_to_aos_expsh
函数)时,我得到一个pexpect超时错误。在
我将_ssh_connect
返回的pexpect对象(_ssh_connect
、ssh_to_aos_expsh
和{ssh_to_aos_expsh
返回的对象进行了比较,并且它似乎位于同一内存位置,所以我不清楚为什么我不能继续使用pexpect操作该对象。在
我不是最复杂的python代码编写者,所以在尝试在函数之间传递pexpect对象时可能犯了一些不经意的错误,如果是这样的话,我希望有人指出我的错误。在
#!/usr/bin/env python
import os
import traceback
import pexpect
class SSHTool():
def __init__(self):
self.aos_user = 'some_user'
self.aos_passwd = 'some_passwd'
self.aos_init_prompt = 'accelerator>'
self.aos_enable_prompt = 'accelerator#'
self.aos_lnxsh_prompt = 'ACC#'
self.linux_passwd = 'linux_passwd'
self.root_prompt = ''
def _timeout_error(self, child):
print 'SSH could not login. Timeout error.'
print child.before, child.after
return None
def _password_error(self, child):
print 'SSH could not login. Password error.'
print child.before, child.after
return None
def _ssh_connect(self, user, address, passwd):
self.root_prompt = "root@%s's password: " % address
ssh_newkey = "Are you sure you want to continue connecting"
child = pexpect.spawn('ssh -l %s %s' % (user, address))
i = child.expect([pexpect.TIMEOUT, \
ssh_newkey, \
'Password: ', \
self.root_prompt])
if i == 0: # Timeout
return self._timeout_error(child)
elif i == 1: # SSH does not have the public key. Just accept it.
child.sendline ('yes')
i = child.expect([pexpect.TIMEOUT, \
'Password: ', \
self.root_prompt])
if i == 0: # Timeout
return self._timeout_error(child)
else:
child.sendline(passwd)
return child
elif i == 2 or i == 3:
child.sendline(passwd)
return child
else:
return self._password_error(child)
def ssh_to_aos_expsh(self, ip_address, command = ''):
child = self._ssh_connect(self.aos_user, \
ip_address, \
self.aos_passwd)
i = child.expect([pexpect.TIMEOUT, \
self.aos_init_prompt])
if i == 0:
return self._timeout_error(child)
child.sendline('enable')
i = child.expect([pexpect.TIMEOUT, \
self.aos_enable_prompt])
if i == 0:
return self._timeout_error(child)
if command:
child.sendline(command)
i = child.expect([pexpect.TIMEOUT, \
self.aos_enable_prompt])
if i == 0:
return self._timeout_error(child)
else:
return child.before
else:
return child
def ssh_to_aos_config(self, ip_address, command):
child = self.ssh_to_aos_expsh(ip_address)
i = child.expect([pexpect.TIMEOUT, \
self.aos_enable_prompt])
if i == 0:
return self._timeout_error(child)
child.sendline('config')
i = child.expect([pexpect.TIMEOUT, \
self.aos_config_prompt])
if i == 0:
return self._timeout_error(child)
child.sendline(command)
i = child.expect([pexpect.TIMEOUT, \
self.aos_config_prompt])
if i == 0:
return self._timeout_error(child)
else:
return child.before
我猜超时是因为
ssh_to_aos_config()
没有得到它期望的所有输入:对ssh_to_aos_expsh()
的调用可能正常工作,而对expect
的后续调用则不能。在所以问题是:超时发生在哪里?您可以通过引发异常而不是返回self.\u timeout\u error(child)来跟踪此情况。找到的位置将指向pexpect永远不会得到的输入(因此会超时),您可以在那里更新代码。在
如果你得到了一个超时,那是因为你没有得到你期望的任何字符串。可能是您收到了一条错误消息,或者您期望的提示是错误的。在
启用日志记录以查看整个交互-在pexpect 2.3中,这是通过向child.log文件属性-然后你就可以看到发生了什么。检查文档中的早期版本,因为我认为这已经改变了。在
我注意到你的代码中有几点:
1)根提示是空字符串。这将始终立即匹配,即使没有从客户端返回任何内容。这可能是问题的原因-ssh connect函数认为它已经看到提示并成功登录,而客户端仍在等待其他输入。在
2)代码中存在语法错误-在ssh\u connect中,您有以下序列:
elif与if语句不匹配,因此AFAIK this永远不会编译。我认为这是一个剪切粘贴错误,因为你说你一直在运行代码。在
结果发现有两个问题,只要我知道问题是什么,都很容易解决。首先,
__init__
方法不包含self.aos_config_prompt
-当我注释掉异常处理代码时,pexpect异常非常清楚地说明了这一点。第二,给定一个看起来像“accelerator(config)#”的self.aos_config_prompt
,pexpect将其编译成re-module匹配代码,然后只匹配包含括号内容的提示。只需转义字符串中的括号,匹配就可以正常工作。在相关问题 更多 >
编程相关推荐