Exscript控制cisco执行'reload'命令(Exscript control cisco execute 'reload' command)

编程入门 行业动态 更新时间:2024-10-25 19:31:36
Exscript控制cisco执行'reload'命令(Exscript control cisco execute 'reload' command)

我最终尝试使用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.response

Any 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.response

You 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

本文发布于:2023-07-04 10:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   reload   cisco   Exscript   execute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!