Python如何實(shí)現(xiàn)網(wǎng)絡(luò)自動(dòng)化運(yùn)維華為設(shè)備
Python網(wǎng)絡(luò)自動(dòng)化運(yùn)維華為設(shè)備
本文將講述使用Python實(shí)現(xiàn)華為設(shè)備的網(wǎng)絡(luò)自動(dòng)化運(yùn)維。
本次運(yùn)維是基于Python 3.11環(huán)境,需要提前安裝paramiko和ncclient第三方庫(kù)。
使用eNSP仿真模擬器來(lái)模擬真實(shí)環(huán)境的拓?fù)鋱D
首先需要保證宿主機(jī)與模擬器內(nèi)的網(wǎng)絡(luò)互通
我這里使用了192.168.10.0/24的本地適配器作為橋接網(wǎng)卡
測(cè)試互通性效果如下:
接下來(lái)配置登錄LSW4交換機(jī)設(shè)備的本地賬戶(hù)
int vlan 1 ip add 192.168.10.2 24 //配置VLANif 1的地址用來(lái)連接設(shè)備 user-interface vty 0 4 authentication-mode aaa //將虛擬遠(yuǎn)程登錄的模式設(shè)置為AAA認(rèn)證 protocol inbound ssh //使能SSH協(xié)議連接功能 user privilege level 15 //用戶(hù)權(quán)限為15,這是最高權(quán)限 quit stelnet server enable //使能設(shè)備stelnet服務(wù)的功能 ssh user test //配置SSH用戶(hù)test ssh user test service-type stelnet //將用戶(hù)的服務(wù)類(lèi)型改為stelnet ssh user test authentication-type password //將SSH用戶(hù)test的登錄模式改為密碼模式 aaa //進(jìn)入AAA視圖 local-user test password cipher Huawei@123 //配置test賬戶(hù)的密碼 local-user test privilege level 15 //配置test賬號(hào)的權(quán)限為15 local-user test service-type ssh //配置SSH賬戶(hù)的權(quán)限為SSH //以上是connect和command模塊 sftp server enable //使能SFTP服務(wù) ssh user test service-type all //開(kāi)啟test全部的服務(wù) ssh user test sftp-directory flash: //將用戶(hù)test的sftp下載目錄指定為flash:下 //以上是download模塊
配置LSW6的聚合鏈路和OSPF協(xié)議用以測(cè)試
int eth-tr 1 mode lacp trunkport g0/0/1 trunkport g0/0/2 //配置聚合鏈路 int vlan 1 ip add 192.168.10.4 24 //使用該地址登錄設(shè)備 ospf area 0 net 0.0.0.0 0.0.0.0 //與LSW4建立OSPF鄰居關(guān)系
LSW4上也要進(jìn)行相應(yīng)OSPF和聚合鏈路的配置
interface Eth-Trunk1 mode lacp-static # interface GigabitEthernet0/0/2 eth-trunk 1 # interface GigabitEthernet0/0/3 eth-trunk 1 //聚合鏈路 ospf 1 area 0.0.0.0 network 0.0.0.0 255.255.255.255 //配置OSPF協(xié)議
接下來(lái)進(jìn)行CE2設(shè)備的配置
該設(shè)備主要用來(lái)進(jìn)行netconf的功能測(cè)試
int vlan 1 ip add 192.168.10.3 24 //配置登錄地址 int g1/0/0 undo shut //開(kāi)啟接口 stelnet server enable //使能stelnet服務(wù) user-interface vty 0 4 authentication-mode aaa protocol inbound ssh //將遠(yuǎn)程登錄模式設(shè)置為AAA模式,開(kāi)啟SSH登錄功能 aaa local-user netconf password cipher Huawei@123 local-user netconf service-type ssh local-user netconf level 3 //創(chuàng)建一個(gè)netconf賬戶(hù),密碼為Huawei@123,服務(wù)類(lèi)型為SSH,權(quán)限為3 # ssh user netconf ssh user netconf authentication-type password ssh user netconf service-type snetconf //將用戶(hù)netconf的認(rèn)證類(lèi)型改為密碼類(lèi)型,服務(wù)類(lèi)型改為snetconf 將 # netconf protocol inbound ssh port 830 //netconf的端口號(hào)為830,允許netconf通過(guò)830號(hào)端口連接 q # snetconf server enable //使能snetconf服務(wù)
下載好第三方庫(kù)
ncclient,paramiko
在PyCharm環(huán)境內(nèi)引入對(duì)應(yīng)的庫(kù)
import re,time,paramiko from datetime import datetime from config import parameter_dict,netconf1 from ncclient import manager from ncclient.xml_ import to_ele from threading import Thread
創(chuàng)建一個(gè)類(lèi)
里面定義并實(shí)現(xiàn)六個(gè)功能模塊:
class SW: def __init__(self): //初始化,建立起與設(shè)備的SSH連接。 self.session = paramiko.SSHClient() self.session.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.session.connect(hostname='192.168.10.2',password='Huawei@123',username='test',port=22,allow_agent=False,look_for_keys=False) self.vty = self.session.invoke_shell() self.command('sc 0 te') print('連接成功') def command(self,command): //向網(wǎng)絡(luò)設(shè)備終端輸入變量并輸出回顯,回顯字符上限為999999,類(lèi)型為utf-8 self.vty.send(command+'\n') time.sleep(1) return self.vty.recv(999999).decode('utf-8') def parameter_info(self): //定義監(jiān)控?cái)?shù)據(jù)參數(shù)方法,每個(gè)5分鐘輸出一次設(shè)備參數(shù) parameter_list = [] while True: for i in parameter_dict.keys(): pat = self.command(parameter_dict[i]['command']) print(pat) res = re.compile(parameter_dict[i]['re']).findall(pat) print(res) if i == 'fan': res = res if res else "All are fans faulty" parameter_list.append(f'{i}:{res}') print('\n'.join(parameter_list)) time.sleep(60 * 5) def download(self): //通過(guò)SFTP功能下載flash:/vrpcfg.zip文件,并每隔一天保存在本地W:\\TestPython文件夾內(nèi) while True: with paramiko.Transport(('192.168.10.2',22)) as t: t.connect(username='test',password='Huawei@123') sftp = paramiko.SFTPClient.from_transport(t) sftp.get('/vrpcfg.zip',f'W:\\TestPython\\{datetime.now().strftime("%Y_%m_%d_%H_%M_%S")}.zip') print('下載成功') time.sleep(60 * 60 * 24) def netconf(self,xml): //最后配置日志文件地址為10.1.60.2 manager.connect(host='192.168.10.3',port=22,username='netconf',password="Huawei@123",allow_agent=False,hostkey_verify=False,device_params={"name":"huawei"}).rpc(to_ele(xml)) print("設(shè)置成功") def start(self): //定義多進(jìn)程模塊 Thread(target=self.download).start() Thread(target=self.parameter_info).start() if __name__ == "__main__": //滿(mǎn)足條件則運(yùn)行該文件 for i in netconf1.values(): SW().start() SW().netconf(i)
需要配置一個(gè)名為config的xml文件
內(nèi)容如下所示,運(yùn)行后會(huì)查看該設(shè)備的風(fēng)扇狀態(tài),電源狀態(tài),CPU利用率,內(nèi)存使用率,OSPF鄰居狀態(tài),以及聚合鏈路狀態(tài)。
parameter_dict = { "fan" : {"command":"display fan","re":"Normal"}, "power" : {"command":"display power","re":"Supply|NotSupply|Sleep|No"}, "cpu" : {"command":"display cpu-usage","re":"CPU Usage : .+?%"}, "memory" : {"command":"display memory","re":"Memory Using Percentage Is: .+?%"}, "ospf" : {"command":"display ospf peer brief","re":"down|init|2-way|exstart|exchange|loading|Full"}, "lacp" : {"command":"display eth-trunk","re":"Up|Down"} } netconf1 = { 'host_log': ''' <edit-config> <target> <running/> </target> <default-operation>merge</default-operation> <error-option>rollback-on-error</error-option> <config> <syslog xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> <syslogServers> <syslogServer operation="merge"> <ipType>ipv4</ipType> <serverIp>10.1.60.2</serverIp> <isDefaultVpn>false</isDefaultVpn> <vrfName>_public_</vrfName> <timestamp>UTC</timestamp> <transportMode>tcp</transportMode> </syslogServer> </syslogServers> </syslog> </config> </edit-config> '''}
運(yùn)行后就可以輸出網(wǎng)絡(luò)設(shè)備終端的回顯內(nèi)容了
以下是回顯截圖:
SFTP本地下載設(shè)備狀態(tài)到指定路徑測(cè)試
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PyQt5實(shí)現(xiàn)tableWidget 居中顯示
這篇文章主要介紹了PyQt5實(shí)現(xiàn)tableWidget 居中顯示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07python標(biāo)識(shí)符的用法及注意事項(xiàng)
在本篇文章里小編給大家整理了一篇關(guān)于python標(biāo)識(shí)符的用法及注意事項(xiàng)相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-09-09圖文詳解梯度下降算法的原理及Python實(shí)現(xiàn)
梯度下降是迭代法的一種,可以用于求解最小二乘問(wèn)題(線性和非線性都可以)。本文將通過(guò)圖文詳解梯度下降算法的原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08Flask框架Jinjia模板常用語(yǔ)法總結(jié)
這篇文章主要介紹了Flask框架Jinjia模板常用語(yǔ)法,結(jié)合實(shí)例形式總結(jié)分析了Jinjia模板的變量、賦值、流程控制、函數(shù)、塊、宏等基本使用方法,需要的朋友可以參考下2018-07-07python中np.random.permutation函數(shù)實(shí)例詳解
np.random.permutation是numpy中的一個(gè)函數(shù),它可以將一個(gè)數(shù)組中的元素隨機(jī)打亂,返回一個(gè)打亂后的新數(shù)組,下面這篇文章主要給大家介紹了關(guān)于python中np.random.permutation函數(shù)的相關(guān)資料,需要的朋友可以參考下2023-04-04python爬蟲(chóng)智能翻頁(yè)批量下載文件的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python爬蟲(chóng)智能翻頁(yè)批量下載文件的實(shí)例詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02