Python语言 netmiko 管理devices简例
小标 2018-12-20 来源 : 阅读 3066 评论 0

摘要:本文主要向大家介绍了Python语言 netmiko 管理devices简例,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

本文主要向大家介绍了Python语言 netmiko 管理devices简例,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

netmiko是python中可用于network device 备份的库,目前支持的设备如下:As of June 2015, Netmiko has support for the following platforms:Cisco IOSCisco IOS-XECisco ASACisco NX-OSCisco IOS-XRCisco WLC (limited testing)Arista EOSHP ProCurveHP Comware (limited testing)Juniper JunosBrocade VDX (limited testing)F5 LTM (experimental)Huawei (limited testing)

下面是一段使用netmiko库登陆ciscorouter 3360 的python代码

from netmiko import ConnectHandler

#要连接设备的信息,顺序不重要

cisco = {    'device_type':'cisco_ios',    'ip':'192.168.60.222',    'username':'admin',    'password': 'password',    'secret':'google',   #enable password}

#进行ssh连接

connect=ConnectHandler(**cisco)     #对于两个*号,我的理解是:ConnectHandler()函数需要在cisco字典里面找"两"个东西,key和对应的value,所以用两个*星号

#如果enable有密码需要在配置连接的时候配置,例如 'secret':'google'  ,输入connect.send_command('enable') 是无效的

connect.enable()   #相当于进入特权模式output_1 = connect.send_command('show run')print("show run 的输出如下:\n"+output_1)print("----------------------")

#输入connect.send_command('config ter')是进不了配置模式的,需要配置直接输入connect.send_config_set('cli')

out_put_2 = connect.send_config_set('ip route 0.0.0.0 0.0.0.0 192.168.60.129')  #相当于输入特权模式 + 输入配置命令print("配置默认路由后的输出:\n\n"+out_put_2+"\n\n")          #输出配置过程

#查看上述的配置是否生效

show_route = connect.send_command('show ip route') #send_command('cli')命令执行的结果只能在本级,不能exit或者 enable、configure terminalprint("查看路由条目:\n"+show_route)
1、show run 的输出如下:Building configuration...Current configuration : 874 bytes!version 12.4service timestamps debug datetime msecservice timestamps log datetime msec logging synchronousline aux 0 exec-timeout 0 0 privilege level 15 logging synchronousline vty 0 4 login local!!end----------------------2、配置默认路由后的输出:config termEnter configuration commands, one per line.  End with CNTL/Z.R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.60.129R1(config)#endR1#3、查看路由条目:Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area        N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2       E1 - OSPF external type 1, E2 - OSPF external type 2       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2       ia - IS-IS inter area, * - candidate default, U - per-user static route       o - ODR, P - periodic downloaded static routeGateway of last resort is 192.168.60.129 to network 0.0.0.0C    192.168.60.0/24 is directly connected, FastEthernet0/0S*   0.0.0.0/0 [1/0] via 192.168.60.129Process finished with exit code 0

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved