王小霞 李騰 楊啟超 肖凝 游閩 王凱睿
DOI:10.19850/j.cnki.2096-4706.2024.01.028
收稿日期:2023-05-16
摘? 要:交換機(jī)自動(dòng)化運(yùn)維是日常工程生產(chǎn)中的一個(gè)普遍問(wèn)題,文章針對(duì)電力公司中廣泛使用的華三交換機(jī)(S5500),將信息運(yùn)維人員日常工作中有關(guān)交換機(jī)的大量重復(fù)性操作指令編寫成Python腳本,解決了多臺(tái)交換機(jī)同時(shí)操作時(shí)的耗時(shí)費(fèi)力問(wèn)題,具體來(lái)說(shuō)就是多臺(tái)交換機(jī)的賬號(hào)密碼不一致,管理復(fù)雜度高;交換機(jī)內(nèi)部不同策略的指令復(fù)雜度不一致,對(duì)運(yùn)維人員要求高等問(wèn)題。交換機(jī)自動(dòng)化運(yùn)維切實(shí)提高了信息運(yùn)維人員的工作效率和操作結(jié)果的準(zhǔn)確性。
關(guān)鍵詞:Python;自動(dòng)化運(yùn)維;交換機(jī);Netmiko
中圖分類號(hào):TP311.1;TP391? 文獻(xiàn)標(biāo)識(shí)碼:A? 文章編號(hào):2096-4706(2024)01-0134-04
Application of Python in Automatic Operation and Maintenance of Switch
WANG Xiaoxia, LI Teng, YANG Qichao, XIAO Ning, YOU Min, WANG Kairui
(State Grid Nanchang Power Supply Company, Nanchang? 330069, China)
Abstract: Automatic operation and maintenance of switches is a common problem in daily engineering production. This paper focuses on the widely used Huasan switch (S5500) in power companies, and compiles a large number of repetitive operation instructions related to switches in the daily work of information operation and maintenance personnel into Python scripts, solving the time-consuming and labor-intensive problem of multiple switches operating simultaneously. Specifically, the account passwords of multiple switches are inconsistent and the management complexity is high; the instruction complexity of different strategies within the switch is inconsistent, which poses high requirements for operation and maintenance personnel. Automatic operation and maintenance of switches has effectively improved the work efficiency and accuracy of operation results of information operation and maintenance personnel.
Keywords: Python; automatic operation and maintenance; switch; Netmiko
0? 引? 言
隨著互聯(lián)網(wǎng)的蓬勃發(fā)展和IT新技術(shù)的不斷涌現(xiàn),人們的生產(chǎn)生活方式逐漸趨于便捷化[1]。在傳統(tǒng)國(guó)網(wǎng)信息運(yùn)維過(guò)程中,交換機(jī)的維護(hù)往往占用工作人員大量的時(shí)間[2]。因此,僅僅依靠手工操作管理公司網(wǎng)絡(luò)設(shè)備的做法已經(jīng)遠(yuǎn)遠(yuǎn)不能滿足信息運(yùn)維人員的日常運(yùn)維需求。
交換機(jī)配置的自動(dòng)化勢(shì)在必行。主要體現(xiàn)在以下幾個(gè)方面:1)多線程執(zhí)行交換機(jī)腳本。2)多臺(tái)交換機(jī)賬號(hào)密碼的批量修改。3)交換機(jī)內(nèi)部組策略的批量制定和執(zhí)行等。這些操作基本上是運(yùn)維人員每天都要做的重復(fù)性工作,采用網(wǎng)絡(luò)自動(dòng)化配置替代傳統(tǒng)的手工配置,其效率更高,成本更低[3]。
本文以公司廣泛使用的華三交換機(jī)(S5500)為例,將信息運(yùn)維人員日常運(yùn)維工作中有關(guān)交換機(jī)的大量重復(fù)性操作指令編寫成Python腳本,實(shí)現(xiàn)批量交換機(jī)的自動(dòng)化操作,有效提高信息運(yùn)維人員的工作效率和操作結(jié)果的準(zhǔn)確性。
1? 問(wèn)題及對(duì)策
問(wèn)題一:多臺(tái)交換機(jī)同時(shí)操作時(shí)耗時(shí)費(fèi)力,人工多開(kāi)進(jìn)程繁雜易亂。
問(wèn)題描述:在傳統(tǒng)的日常網(wǎng)絡(luò)維護(hù)工作中,信息運(yùn)維人員需要手動(dòng)配置n臺(tái)交換機(jī),首先登錄SecureCRT,輸入交換機(jī)的管理地址,選擇通過(guò)遠(yuǎn)程登錄服務(wù)的標(biāo)準(zhǔn)協(xié)議SSH進(jìn)行登錄(此過(guò)程大約耗時(shí)10~15秒);然后在登錄界面中輸入交換機(jī)的用戶名和密碼,運(yùn)行配置命令,保存配置后退出交換機(jī)(此過(guò)程大約耗時(shí)3~5分鐘);接下來(lái)對(duì)下一臺(tái)交換機(jī)執(zhí)行類似操作。
解決對(duì)策:利用Python中第三方庫(kù)Gevent實(shí)現(xiàn)不同交換機(jī)多線程并發(fā)處理。該函數(shù)的優(yōu)點(diǎn)就是便于切換控制流,簡(jiǎn)化編程模型。針對(duì)不同的交換機(jī),只需導(dǎo)入交換機(jī)的IP地址表,一鍵執(zhí)行腳本文件并發(fā)處理,大大提升了工作效率。
問(wèn)題二:多臺(tái)交換機(jī)的賬號(hào)密碼不一致,管理復(fù)雜度高。
問(wèn)題描述:在傳統(tǒng)的日常網(wǎng)絡(luò)維護(hù)工作中,不同交換機(jī)的賬號(hào)密碼往往由多名信息運(yùn)維人員創(chuàng)建和修改,而不同運(yùn)維人員更換密碼的時(shí)間不同,容易導(dǎo)致多臺(tái)交換機(jī)存在同一時(shí)間密碼不一致的問(wèn)題,這樣會(huì)給后續(xù)運(yùn)維人員帶來(lái)不必要的麻煩。
解決對(duì)策:利用Python中第三方庫(kù)Netmiko實(shí)現(xiàn)同一時(shí)間批量修改不同交換機(jī)的賬號(hào)密碼。該函數(shù)的優(yōu)點(diǎn)就是便于在交換機(jī)上批量處理指令[4]。針對(duì)不同的交換機(jī),只需導(dǎo)入交換機(jī)的IP地址表,指定一名專員定期執(zhí)行腳本文件,從而減少其他運(yùn)維人員的工作量。
問(wèn)題三:交換機(jī)內(nèi)部不同策略的指令復(fù)雜度不一致,對(duì)運(yùn)維人員要求高。
問(wèn)題描述:在傳統(tǒng)的日常網(wǎng)絡(luò)維護(hù)工作中,需要對(duì)多個(gè)交換機(jī)的某一端口進(jìn)行關(guān)閉,以及對(duì)不同信息運(yùn)維人員的終端IP進(jìn)行管理[5]。這些配置的復(fù)雜度不一,給運(yùn)維人員的工作帶來(lái)困擾。
解決對(duì)策:利用Python中第三方庫(kù)Netmiko實(shí)現(xiàn)批量修改配置交換機(jī)策略。該函數(shù)的優(yōu)點(diǎn)就是便于在交換機(jī)上批量處理指令。針對(duì)不同策略的配置寫成不同函數(shù),只需導(dǎo)入IP端口對(duì)應(yīng)表或ACL用戶權(quán)限表等即可實(shí)現(xiàn)批量處理指令。模塊化的設(shè)計(jì)理念,使得后續(xù)功能可隨意擴(kuò)展,提升了代碼的適用性。
2? 典型做法
2.1? 框架結(jié)構(gòu)
本文針對(duì)公司大量網(wǎng)絡(luò)設(shè)備需要重復(fù)性配置的情況,基于Python開(kāi)源庫(kù)Gevent、Netmiko設(shè)計(jì)如圖1所示的自動(dòng)化操作流程。該自動(dòng)化操作流程通過(guò)Python編程方式實(shí)現(xiàn)對(duì)人工登錄網(wǎng)絡(luò)設(shè)備配置參數(shù)的計(jì)算機(jī)模擬[6],實(shí)現(xiàn)自動(dòng)化的參數(shù)配置。
如圖1所示,該自動(dòng)化操作過(guò)程分為六個(gè)階段:1)讀取txt數(shù)據(jù)文件解析生成模板,主要借助Python內(nèi)置函數(shù)open讀取IP列表文件,作為腳本進(jìn)行輸入。2)實(shí)現(xiàn)交換機(jī)多線程并發(fā)處理,主要借助Python中第三方庫(kù)Gevent多線程執(zhí)行配置命令。3)建立Python與交換機(jī)的連接,主要借助Python中第三方庫(kù)Netmiko實(shí)現(xiàn)后臺(tái)操作交換機(jī)。4)實(shí)現(xiàn)批量同步修改登錄密碼。5)實(shí)現(xiàn)封端口、綁定IP-MAC等批量修改交換機(jī)策略配置。6)保存配置同時(shí)備份配置日志,便于日后維護(hù)和審計(jì)溯源。
整體代碼結(jié)構(gòu)如圖2所示,包含read_device_txt()、Login_Multithread()、change_ Password()、strategy_Banport()、backup_Config()等函數(shù)。
2.2? 編碼思路
編寫函數(shù)read_device_txt(),讀取ip_lists.txt文件中交換機(jī)的管理IP、用戶名和密碼,利用Python中set集合生成形如(IP、username、password)格式的三元組數(shù)據(jù),以便后續(xù)多線程讀取數(shù)據(jù),優(yōu)化輸入。
簡(jiǎn)單的代碼示例如下:
defread_device_txt():
# 讀取iplist.txt 里面的ip地址
ipread = open('iplist.txt', 'r', encoding='ascii')
ip_addr = ipread.readlines()
ipread.close()
iplist = []
for tmp in ip_addr:
tmp = tmp.replace("\n", "")
ip = tmp.split("")[0]
username = tmp.split("")[1]
password = tmp.split("")[2]
iptup = (ip, username, password)
iplist.append(iptup)
return iplist
利用Python中ThreadPoolExecutor模塊實(shí)現(xiàn)多線程并發(fā),每個(gè)線程后臺(tái)開(kāi)啟一個(gè)交換機(jī),可同時(shí)開(kāi)啟n(n≥2)個(gè)交換機(jī)進(jìn)行處理,這樣可節(jié)約大量時(shí)間:
defmain():
starting_time = time()
ip_list = read_device_txt()
#? 異步線程ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=20) as executor:
result = executor.map(run_gevent, ip_list)
print('\n---- End get config threading, elapsed time=', time() - starting_time)
利用Python中第三方庫(kù)Netmiko的ConnectHandler()模塊,實(shí)現(xiàn)基于SSH遠(yuǎn)程控制Python與交換機(jī)的連接,編寫函數(shù)run_gevent(),實(shí)現(xiàn)交換機(jī)多線程并行處理數(shù)據(jù):
defrun_gevent(ipaddr):
issue_device = []
ip = ipaddr[0]
username = ipaddr[1]
password = ipaddr[2]
try:
S5130 = {
'device_type': 'hp_comware',
'ip': ip,
'username': username,
'password': password
}
net_connect = ConnectHandler(**S5130)
sucessloginip = ip
print('sucess: '+ sucessloginip)
except (AuthenticationException):
issue_message = (ipaddr +': 認(rèn)證錯(cuò)誤 ')
issue_device.append(issue_message)
except NetMikoTimeoutException:
issue_message = (ipaddr +': 網(wǎng)絡(luò)不可達(dá) ')
issue_device.append(issue_message)
except (SSHException):
issue_message = (ipaddr +': SSH端口異常 ')
issue_device.append(issue_message)
exceptExceptionas unknown_error:
issue_message = (ipaddr +': 發(fā)生未知錯(cuò)誤: ')
issue_device.append(issue_message + str(unknown_error))
編寫函數(shù)changePassword (),實(shí)現(xiàn)批量修改交換機(jī)登錄密碼,該操作僅需一名運(yùn)維人員定期執(zhí)行腳本即可實(shí)現(xiàn)全網(wǎng)交換機(jī)密碼同步,大大節(jié)約了人工成本:
defchangePassword(ip, username, password):
S5130 = {
'device_type': 'hp_comware',
'ip': ip,
'username': username,
'password': password
}
net_connect = ConnectHandler(**S5130)
cmd = ['undo password-control enable']? # 取消密碼策略
cmd_result1 = net_connect.send_config_set(cmd)
cmd2 = ['local-user ngwl', 'display this', 'password simple xxxxxx']
cmd_result2 = net_connect.send_config_set(cmd2)
cmd_result3 = net_connect.save_config()? # 保存配置
net_connect.disconnect()
return ip
編寫函數(shù)strategy_Banport(),通過(guò)設(shè)置高危端口的ACL策略,實(shí)現(xiàn)批量封閉高危端口,保障網(wǎng)絡(luò)安全:
defstrategy_Banport(ip, username, password):
S5130 = {
'device_type': 'hp_comware',
'ip': ip,
'username': username,
'password': password
}
net_connect = ConnectHandler(**S5130)
cmd = ['acl number 3008',
'rule 1 deny tcp source-port eq 445',
'rule 2 deny tcp destination-port eq 445',
'rule 3 deny udp destination-port eq 445',
'rule 4 deny udp source-port eq 445',
'rule 5 deny tcp source-port eq 135',
'rule 6 deny tcp destination-port eq 135',
'rule 7 deny udp source-port eq 135',
'rule 8 deny udp destination-port eq 135',
'rule 9 deny udp destination-port eq 137',
'rule 10 deny udp source-port eq 137',
'rule 11 deny udp destination-port eq 138',
'rule 12 deny udp source-port eq 138',
'rule 13 deny udp destination-port eq 139',
'rule 14 deny udp source-port eq 139',
]? # 高危端口封禁
cmd_result1 = net_connect.send_config_set(cmd)
cmd2 = ['traffic classifier banPort operator and','if-match acl 3008']
cmd_result2 = net_connect.send_config_set(cmd2)
cmd3 = ['traffic behavior banPort','deny']
cmd_result3 = net_connect.send_config_set(cmd3)
cmd4 = ['traffic policy banPort','classifier banPort behavior banPort']
cmd_result4 = net_connect.send_config_set(cmd4)
cmd5 = ['qos apply policy banPort inbound','qos apply policy banPort outbound']
cmd_result5 = net_connect.send_config_set(cmd5)
cmd_result = net_connect.save_config()? # 保存配置
net_connect.disconnect()
return ip
最后,利用Netmiko庫(kù)的save_config()方法后臺(tái)保存修改過(guò)的交換機(jī)配置,同時(shí)使用backupConfig()函數(shù)進(jìn)行數(shù)據(jù)備份,便于日后維護(hù)和審計(jì)溯源:
defbackupConfig(ip, username, password):
S5130 = {
'device_type': 'hp_comware',
'ip': ip,
'username': username,
'password': password
}
net_connect = ConnectHandler(**S5130)
commands = ['display current-configuration']? # dis cu
for cmd in commands:
cmd_result = net_connect.send_config_set(cmd)
timestr = time.strftime('%Y-%m-%d', time.localtime(time.time()))
filename =u'%s_%s_%s.txt'% (ip, cmd.replace(' ', '_'), timestr)
configsw = open(filename, 'w', encoding='ascii')
configsw.write(cmd_result)
print('命令執(zhí)行完畢,結(jié)果保存于當(dāng)前目錄%s中?。躰'+ filename)
cmd_result3 = net_connect.save_config()? # 保存配置
net_connect.disconnect()
return ip
3? 效率分析
本文使用200臺(tái)H3C(S5500)交換機(jī)進(jìn)行對(duì)比,在將傳統(tǒng)方法與本文提出的使用Python腳本對(duì)交換機(jī)進(jìn)行相同操作時(shí),比較二者所需時(shí)間的不同[7],詳情如表1所示。由表1可知,使用Python腳本進(jìn)行交換機(jī)運(yùn)維時(shí),所需的時(shí)間成本和人力成本遠(yuǎn)遠(yuǎn)低于傳統(tǒng)方法。
4? 結(jié)? 論
本文利用IT領(lǐng)域炙手可熱的Python編程語(yǔ)言實(shí)現(xiàn)了多臺(tái)交換機(jī)配置的自動(dòng)化處理,符合當(dāng)今由繁入簡(jiǎn)的生活趨勢(shì),順應(yīng)AI引領(lǐng)生活的時(shí)代潮流。本文將繁雜的操作編寫成Python腳本,僅需一名運(yùn)維人員定期批量同步修改登錄密碼,實(shí)現(xiàn)了不同交換機(jī)的并發(fā)處理、批量一鍵執(zhí)行不同交換機(jī)策略等功能。多臺(tái)交換機(jī)的自動(dòng)化操作,減少了運(yùn)維人員的工作量和時(shí)間成本,且功能模塊可擴(kuò)展,操作日志可備份留存,便于后續(xù)二次開(kāi)發(fā)和故障溯源。
參考文獻(xiàn):
[1] 葉磊,文濤,劉立亮,等.基于python的網(wǎng)絡(luò)及信息系統(tǒng)安全過(guò)程管理工具 [J].數(shù)字技術(shù)與應(yīng)用,2017(10):187-188.
[2] BOUNPASONG P.基于Python的網(wǎng)絡(luò)系統(tǒng)自動(dòng)化配置方案研究 [D].蘭州:蘭州交通大學(xué),2020.
[3] 魏小梅,傅林,苗晶晶.一種基于命令的網(wǎng)絡(luò)設(shè)備自動(dòng)化運(yùn)維工具 [J].科學(xué)技術(shù)創(chuàng)新,2021(32):96-98.
[4] 李朝陽(yáng),周波,張小鋒,等.一種基于Netmiko的網(wǎng)絡(luò)設(shè)備自動(dòng)化運(yùn)維方法研究 [J].通信技術(shù),2021,54(3):773-778.
[5] 劉祥.網(wǎng)絡(luò)設(shè)備自動(dòng)化巡檢運(yùn)維的實(shí)現(xiàn)與應(yīng)用研究 [J].信息系統(tǒng)工程,2019(6):92.
[6] 鎖泉凝.基于Python的園區(qū)網(wǎng)絡(luò)自動(dòng)化運(yùn)維 [J].長(zhǎng)江信息通信,2021,34(6):74-75+78.
[7] 楊迎,李慧穎.Python在網(wǎng)絡(luò)運(yùn)維中的應(yīng)用 [J].現(xiàn)代信息科技,2020,4(4);122-124.
作者簡(jiǎn)介:王小霞(1994—),女,漢族,江西南昌人,助理工程師,碩士,研究方向:信息安全;李騰(1992—),女,漢族,江西南昌人,工程師,碩士,研究方向:計(jì)算機(jī)網(wǎng)絡(luò);楊啟超(1989—),男,漢族,江西南昌人,高級(jí)工程師,碩士,研究方向:計(jì)算機(jī)網(wǎng)絡(luò);肖凝(1997—),女,漢族,江西南昌人,助理工程師,碩士,研究方向:計(jì)算機(jī)網(wǎng)絡(luò);游閩(1984—),女,漢族,江西南昌人,高級(jí)工程師,碩士,研究方向:計(jì)算機(jī)網(wǎng)絡(luò);王凱睿(1975—),女,漢族,江西南昌人,高級(jí)工程師,碩士,研究方向:計(jì)算機(jī)網(wǎng)絡(luò)。