我最终尝试使用Exscript创建一些基本功能来控制我的cisco测试实验室设备。 到目前为止,Exscript已经为我做了一切,我只是举几个函数。
我遇到的问题是创建这个reload_start()函数,该函数执行reload命令,重新启动我的Cisco设备并恢复我在测试运行中所做的任何更改。 我已经尝试了几十种不同的字符串组合来执行,但无法让它解决输入'reload'时出现的其他提示
相比之下,我的copy_to_running_config()函数工作正常,只需在字符串末尾添加'\ n'即可。
我还没有跳进Exscript的提示函数(get_prompt(),expect_prompt(),waitfor()等),我猜这是我需要探索的途径,但我找不到这个处理的例子我的具体目标。
from Exscript import Account from Exscript.protocols import Telnet from Exscript.protocols.drivers import ios def __init__(self, ip): self.ip = ip self.account = Account('admin', 'key') self.conn = Telnet() self.conn.set_driver('ios') self.conn.connect(ip) self.conn.login(self.account) self.conn.execute('terminal length 0') def enable(self): self.conn.execute('enable') def copy_to_running_config(self, config): self.enable() self.conn.execute('copy flash:{0} running-config \n'.format(config)) res = self.conn.response self.conn.execute('exit') return res def reload_start(self): self.enable() self.conn.execute('reload \n no \n') print self.conn.response任何帮助或输入将不胜感激!
I'm ultimately trying to create some basic functions to control my cisco test lab devices using Exscript. Exscript has done everything perfectly for me up to this point and I'm just pulling a few functions for example.
What I'm having trouble with is creating this reload_start() function that executes the reload command, rebooting my Cisco device and reverting any changes I've made in my test run. I've tried dozens of different combinations of character strings to execute but cannot get it to work around the additional prompts that come up when typing 'reload'
In contrast, my copy_to_running_config() function works just fine, simply by adding '\n' at the end of the string.
I've not yet jumped into Exscript's prompt functions(get_prompt(), expect_prompt(), waitfor(), etc), and I'm guessing this is the avenue I need to explore, but I cannot find examples of this that deal with my specific goal.
from Exscript import Account from Exscript.protocols import Telnet from Exscript.protocols.drivers import ios def __init__(self, ip): self.ip = ip self.account = Account('admin', 'key') self.conn = Telnet() self.conn.set_driver('ios') self.conn.connect(ip) self.conn.login(self.account) self.conn.execute('terminal length 0') def enable(self): self.conn.execute('enable') def copy_to_running_config(self, config): self.enable() self.conn.execute('copy flash:{0} running-config \n'.format(config)) res = self.conn.response self.conn.execute('exit') return res def reload_start(self): self.enable() self.conn.execute('reload \n no \n') print self.conn.responseAny help or input would be greatly appreciated!
最满意答案
您的函数reload_start应如下所示:
def reload_start(self): self.enable() self.conn.set_prompt(r'Save\? \[yes/no\]\:') self.conn.execute('reload') self.conn.set_prompt(r'Proceed with reload\? \[confirm\]') self.conn.execute('no') self.conn.set_prompt() self.conn.execute('confirm') print self.conn.response在执行命令之前,必须为提示设置正则表达式。 否则,Exscrpt无法确定何时发送下一个命令。
然而,如果配置没有改变并且路由器没有要求您保存,则上面的脚本将无法工作,因为它会等待保存问题。
Your Function reload_start should look like this:
def reload_start(self): self.enable() self.conn.set_prompt(r'Save\? \[yes/no\]\:') self.conn.execute('reload') self.conn.set_prompt(r'Proceed with reload\? \[confirm\]') self.conn.execute('no') self.conn.set_prompt() self.conn.execute('confirm') print self.conn.responseYou have to set the Regular Expression for the prompt before executing the command. Otherwise Exscrpt is not be able to determine when to send the next command.
Nevertheless, if the configuration wasn't changed and the router doesn't ask you to save, the script above would not work because it would wait for the save-question.
更多推荐
Exscript,self,conn,execute,I'm,电脑培训,计算机培训,IT培训"/> <meta name
发布评论