基于python netmiko模塊實現(xiàn)對華三設備的管理
1、拓撲圖及試驗環(huán)境
華三路由器使用官方模擬器(HCL)實現(xiàn),其中使用host(本地主機)與主機物理網(wǎng)卡進行橋接。Python安裝在真實主機的一個Vmware虛擬機中,虛擬機也采用橋接模式與物理網(wǎng)卡橋接。
2、python腳本
導入netmiko模塊前需要安裝此模塊:pip install netmiko
#導入netmiko模塊 importnetmiko #以字典形式定義登錄信息 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函數(shù)連接路由器 connection1=netmiko.ConnectHandler(**R1) connection2=netmiko.ConnectHandler(**R2) connection3=netmiko.ConnectHandler(**R3) #使用netmiko的send_command函數(shù)在路由器上執(zhí)行命令: output1=connection1.send_command('displayipinterfacebrief',strip_command=False,strip_prompt=False) output2=connection2.send_command('displayipinterfacebrief',strip_command=False,strip_prompt=False) output3=connection3.send_command('displayipinterfacebrief',strip_command=False,strip_prompt=False) print(connection1.send_command('displayipinterfacebrief',strip_command=False,strip_prompt=False)) print(connection1.send_command('displayversion',strip_command=False,strip_prompt=False)) print(connection1.send_command('displayinterfacebrief',strip_command=False,strip_prompt=False)) print(connection1.send_command('displayarp',strip_command=False,strip_prompt=False)) print(connection1.send_command('displaymac-address',strip_command=False,strip_prompt=False)) print(connection1.send_command('displaylogbuffer',strip_command=False,strip_prompt=False))
輸出效果如下:
displayipinterfacebrief *down:administrativelydown (s):spoofing(l):loopback InterfacePhysicalProtocolIPAddressDescription GE0/0upup10.0.0.10-- GE0/1downdown---- GE0/2downdown---- GE5/0downdown---- GE5/1downdown---- GE6/0downdown---- GE6/1downdown---- Ser1/0downdown---- Ser2/0downdown---- Ser3/0downdown---- Ser4/0downdown----displayversion H3CComwareSoftware,Version7.1.075,Alpha7571 Copyright(c)2004-2017NewH3CTechnologiesCo.,Ltd.Allrightsreserved. H3CMSR36-20uptimeis0weeks,0days,0hours,35minutes Lastrebootreason:Userreboot Bootimage:flash:/msr36-cmw710-boot-a7514.bin Bootimageversion:7.1.075,Alpha7571 CompiledSep20201716:00:00 Bootimage:flash:/msr36-cmw710-system-a7514.bin Bootimageversion:7.1.075,Alpha7571 CompiledSep20201716:00:00 CPUID:0x2 512MbytesDDR3SDRAMMemory 1024MbytesFlashMemory PCBVersion:2.0 CPLDVersion:1.0 BasicBootWareVersion:1.42 ExtendedBootWareVersion:1.42 displayinterfacebrief Briefinformationoninterfacesinroutemode: Link:ADM-administrativelydown;Stby-standby Protocol:(s)-spoofing InterfaceLinkProtocolPrimaryIPDescription GE0/0UPUP10.0.0.10 GE0/1DOWNDOWN-- GE0/2DOWNDOWN-- GE5/0DOWNDOWN-- GE5/1DOWNDOWN-- GE6/0DOWNDOWN-- GE6/1DOWNDOWN-- InLoop0UPUP(s)-- NULL0UPUP(s)-- REG0UP---- Ser1/0DOWNDOWN-- Ser2/0DOWNDOWN-- Ser3/0DOWNDOWN-- Ser4/0DOWNDOWN-- displayarp Type:S-StaticD-DynamicO-OpenflowR-RuleM-MultiportI-Invalid IPaddressMACaddressSVLAN/VSIInterface/LinkIDAgingType 10.0.0.11861e-ad2e-0205--GE0/05D 10.0.0.12861e-b619-0305--GE0/06D 10.0.0.141000c-294d-bc76--GE0/019D 10.0.0.1636400-6a8a-8016--GE0/020D 10.0.0.25450d2-f57d-19f6--GE0/020D displaymac-address MACAddressVLANIDStatePort/NicknameAging displaylogbuffer Logbuffer:Enabled Maxbuffersize:1024 Actualbuffersize:512 Droppedmessages:0 Overwrittenmessages:0 Currentmessages:78 %Nov2320:15:25:3322020R1SYSLOG/6/SYSLOG_RESTART:Systemrestarted-- H3CComwareSoftware. %Nov2320:15:33:4492020R1IFNET/3/PHY_UPDOWN:PhysicalstateontheinterfaceGigabitEthernet0/0changedtoup. %Nov2320:15:33:4502020R1IFNET/5/LINK_UPDOWN:LineprotocolstateontheinterfaceGigabitEthernet0/0changedtoup. %Nov2320:15:43:5012020R1SHELL/5/SHELL_LOGIN:Consoleloggedinfromcon0. %Nov2320:15:44:6602020R1SHELL/6/SHELL_CMD:-Line=con0-IPAddr=**-User=**;Commandissy %Nov2320:15:51:6692020R1SHELL/6/SHELL_CMD:-Line=con0-IPAddr=**-User=**;CommandisinterfaceGigabitEthernet0/0 %Nov2320:15:58:2452020R1SHELL/6/SHELL_CMD:-Line=con0-IPAddr=**-User=**;Commandisipaddress10.0.0.1024 %Nov2320:16:01:4772020R1SHELL/6/SHELL_CMD:-Line=con0-IPAddr=**-User=**;Commandisping10.0.0.254 %Nov2320:16:02:2952020R1PING/6/PING_STATISTICS:Pingstatisticsfor10.0.0.254:5packet(s)transmitted,5packet(s)received,0.0%packetloss,round-tripmin/avg/max/std-dev=0.000/1.000/2.000/0.632ms.
審核編輯:湯梓紅
-
模塊
+關注
關注
7文章
2670瀏覽量
47340 -
路由器
+關注
關注
22文章
3707瀏覽量
113536 -
虛擬機
+關注
關注
1文章
908瀏覽量
28090 -
python
+關注
關注
56文章
4782瀏覽量
84452 -
華三
+關注
關注
0文章
12瀏覽量
5438
原文標題:基于python netmiko模塊實現(xiàn)對華三設備的管理
文章出處:【微信號:網(wǎng)絡技術干貨圈,微信公眾號:網(wǎng)絡技術干貨圈】歡迎添加關注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關推薦
評論