前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python netmiko 管理 ne

Python netmiko 管理 ne

作者头像
py3study
发布2020-01-07 10:33:20
1.1K0
发布2020-01-07 10:33:20
举报
文章被收录于专栏:python3
代码语言:javascript
复制
netmiko是python中可用于network device 备份的库,目前支持的设备如下:
As of June 2015, Netmiko has support for the following platforms:
Cisco IOS
Cisco IOS-XE
Cisco ASA
Cisco NX-OS
Cisco IOS-XR
Cisco WLC (limited testing)
Arista EOS
HP ProCurve
HP Comware (limited testing)
Juniper Junos
Brocade VDX (limited testing)
F5 LTM (experimental)
Huawei (limited testing)

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

代码语言:javascript
复制
from netmiko import ConnectHandler

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

代码语言:javascript
复制
cisco = {
    'device_type':'cisco_ios',
    'ip':'192.168.60.222',
    'username':'admin',
    'password': 'password',
    'secret':'google',   #enable password
}

#进行ssh连接

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

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

代码语言:javascript
复制
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')

代码语言:javascript
复制
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")          #输出配置过程

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

代码语言:javascript
复制
show_route = connect.send_command('show ip route') #send_command('cli')命令执行的结果只能在本级,不能exit或者 enable、configure terminal
print("查看路由条目:\n"+show_route)
代码语言:javascript
复制
——————————————————————————————————————————————————————————————————————————
上述文件的输出如下(因篇幅问题,有部分会有删除):
代码语言:javascript
复制
1、show run 的输出如下:
Building configuration...

Current configuration : 874 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec

 logging synchronous
line aux 0
 exec-timeout 0 0
 privilege level 15
 logging synchronous
line vty 0 4
 login local
!
!
end

----------------------

2、配置默认路由后的输出:

config term
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#ip route 0.0.0.0 0.0.0.0 192.168.60.129
R1(config)#end
R1#

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 route

Gateway of last resort is 192.168.60.129 to network 0.0.0.0

C    192.168.60.0/24 is directly connected, FastEthernet0/0
S*   0.0.0.0/0 [1/0] via 192.168.60.129

Process finished with exit code 0

参考链接: ①https://pypi.python.org/pypi/netmiko/1.4.1 ②https://pynet.twb-tech.com/blog/automation/netmiko.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档