基于python netmiko模块实现对华三设备的管理
1、拓扑图及试验环境
华三路由器使用官方模拟器(HCL)实现,其中使用host(本地主机)与主机物理网卡进行桥接。Python安装在真实主机的一个Vmware虚拟机中,虚拟机也采用桥接模式与物理网卡桥接。
2、python脚本
导入netmiko模块前需要安装此模块:pip install netmiko
#导入netmiko模块 import netmiko #以字典形式定义登录信息 R1 = {'device_type':'hp_comware','ip':'10.0.0.10','username':'test','password':'abc'} R2 = {'device_type':'hp_comware','ip':'10.0.0.11','username':'test','password':'abc'} R3 = {'device_type':'hp_comware','ip':'10.0.0.12','username':'test','password':'abc'} #使用netmiko的ConnectHandler函数连接路由器 connection1 = netmiko.ConnectHandler(**R1) connection2 = netmiko.ConnectHandler(**R2) connection3 = netmiko.ConnectHandler(**R3) #使用netmiko的send_command函数在路由器上执行命令: output1 = connection1.send_command('display ip interface brief',strip_command=False,strip_prompt=False) output2 = connection2.send_command('display ip interface brief',strip_command=False,strip_prompt=False) output3 = connection3.send_command('display ip interface brief',strip_command=False,strip_prompt=False) print(connection1.send_command('display ip interface brief',strip_command=False,strip_prompt=False)) print(connection1.send_command('display version',strip_command=False,strip_prompt=False)) print(connection1.send_command('display interface brief',strip_command=False,strip_prompt=False)) print(connection1.send_command('display arp',strip_command=False,strip_prompt=False)) print(connection1.send_command('display mac-address',strip_command=False,strip_prompt=False)) print(connection1.send_command('display logbuffer',strip_command=False,strip_prompt=False))
输出效果如下:
display ip interface brief *down: administratively down (s): spoofing (l): loopback Interface Physical Protocol IP Address Description GE0/0 up up 10.0.0.10 -- GE0/1 down down -- -- GE0/2 down down -- -- GE5/0 down down -- -- GE5/1 down down -- -- GE6/0 down down -- -- GE6/1 down down -- -- Ser1/0 down down -- -- Ser2/0 down down -- -- Ser3/0 down down -- -- Ser4/0 down down -- --display version H3C Comware Software, Version 7.1.075, Alpha 7571 Copyright (c) 2004-2017 New H3C Technologies Co., Ltd. All rights reserved. H3C MSR36-20 uptime is 0 weeks, 0 days, 0 hours, 35 minutes Last reboot reason: User reboot Boot image: flash:/msr36-cmw710-boot-a7514.bin Boot image version: 7.1.075, Alpha 7571 Compiled Sep 20 2017 16:00:00 Boot image: flash:/msr36-cmw710-system-a7514.bin Boot image version: 7.1.075, Alpha 7571 Compiled Sep 20 2017 16:00:00 CPU ID: 0x2 512M bytes DDR3 SDRAM Memory 1024M bytes Flash Memory PCB Version: 2.0 CPLD Version: 1.0 Basic BootWare Version: 1.42 Extended BootWare Version: 1.42 display interface brief Brief information on interfaces in route mode: Link: ADM - administratively down; Stby - standby Protocol: (s) - spoofing Interface Link Protocol Primary IP Description GE0/0 UP UP 10.0.0.10 GE0/1 DOWN DOWN -- GE0/2 DOWN DOWN -- GE5/0 DOWN DOWN -- GE5/1 DOWN DOWN -- GE6/0 DOWN DOWN -- GE6/1 DOWN DOWN -- InLoop0 UP UP(s) -- NULL0 UP UP(s) -- REG0 UP -- -- Ser1/0 DOWN DOWN -- Ser2/0 DOWN DOWN -- Ser3/0 DOWN DOWN -- Ser4/0 DOWN DOWN -- display arp Type: S-Static D-Dynamic O-Openflow R-Rule M-Multiport I-Invalid IP address MAC address SVLAN/VSI Interface/Link ID Aging Type 10.0.0.11 861e-ad2e-0205 -- GE0/0 5 D 10.0.0.12 861e-b619-0305 -- GE0/0 6 D 10.0.0.141 000c-294d-bc76 -- GE0/0 19 D 10.0.0.163 6400-6a8a-8016 -- GE0/0 20 D 10.0.0.254 50d2-f57d-19f6 -- GE0/0 20 D display mac-address MAC Address VLAN ID State Port/Nickname Aging display logbuffer Log buffer: Enabled Max buffer size: 1024 Actual buffer size: 512 Dropped messages: 0 Overwritten messages: 0 Current messages: 78 %Nov 23 20:15:25:332 2020 R1 SYSLOG/6/SYSLOG_RESTART: System restarted -- H3C Comware Software. %Nov 23 20:15:33:449 2020 R1 IFNET/3/PHY_UPDOWN: Physical state on the interface GigabitEthernet0/0 changed to up. %Nov 23 20:15:33:450 2020 R1 IFNET/5/LINK_UPDOWN: Line protocol state on the interface GigabitEthernet0/0 changed to up. %Nov 23 20:15:43:501 2020 R1 SHELL/5/SHELL_LOGIN: Console logged in from con0. %Nov 23 20:15:44:660 2020 R1 SHELL/6/SHELL_CMD: -Line=con0-IPAddr=**-User=**; Command is sy %Nov 23 20:15:51:669 2020 R1 SHELL/6/SHELL_CMD: -Line=con0-IPAddr=**-User=**; Command is interface GigabitEthernet 0/0 %Nov 23 20:15:58:245 2020 R1 SHELL/6/SHELL_CMD: -Line=con0-IPAddr=**-User=**; Command is ip address 10.0.0.10 24 %Nov 23 20:16:01:477 2020 R1 SHELL/6/SHELL_CMD: -Line=con0-IPAddr=**-User=**; Command is ping 10.0.0.254 %Nov 23 20:16:02:295 2020 R1 PING/6/PING_STATISTICS: Ping statistics for 10.0.0.254: 5 packet(s) transmitted, 5 packet(s) received, 0.0% packet loss, round-trip min/avg/max/std-dev = 0.000/1.000/2.000/0.632 ms.
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !