日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

如何寫批量備份交換機(jī)配置的Python腳本?5個(gè)廠商安排上!

 copy_left 2023-04-10 發(fā)布于四川

你好,這里是網(wǎng)絡(luò)技術(shù)聯(lián)盟站。

在昨天的文章中,我們介紹了20個(gè)華為路由器常用的Python腳本:

  • 20個(gè)華為路由器常用的Python腳本,網(wǎng)工寫自動(dòng)化腳本時(shí)候可以參考!

文章末尾的評(píng)論區(qū)中,有小伙伴提出想要一下批量備份交換機(jī)配置的腳本:

文章圖片1

既然沒有提出要哪個(gè)廠商,今天瑞哥就安排多廠商的腳本:

文章圖片2

下面讓我們直接開始!

一、華為

import paramikoimport timeimport os# 創(chuàng)建SSH客戶端對(duì)象ssh = paramiko.SSHClient()# 自動(dòng)添加主機(jī)密鑰ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH連接信息username = 'username' # SSH用戶名password = 'password' # SSH密碼port = 22 # SSH端口號(hào)# 讀取交換機(jī)列表with open('switch_list.txt') as f: switch_list = f.read().splitlines()# 遍歷交換機(jī)列表,備份配置文件for switch_ip in switch_list: print(f'正在備份交換機(jī) {switch_ip} 的配置文件...') # 建立SSH連接 ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10) # 發(fā)送命令 ssh.exec_command('system-view') time.sleep(1) ssh.exec_command('backup configuration to tftp 10.0.0.1 config.cfg') # 等待備份完成 time.sleep(5) # 關(guān)閉SSH連接 ssh.close() # 保存?zhèn)浞菸募? filename = f'{switch_ip}.cfg' os.system(f'tftp -g -r config.cfg 10.0.0.1') os.rename('config.cfg', filename) print(f'交換機(jī) {switch_ip} 的配置文件備份完成,保存為 {filename}。')

二、H3C

import paramikoimport timeimport os# 創(chuàng)建SSH客戶端對(duì)象ssh = paramiko.SSHClient()# 自動(dòng)添加主機(jī)密鑰ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH連接信息username = 'username'  # SSH用戶名password = 'password'  # SSH密碼port = 22  # SSH端口號(hào)# 讀取交換機(jī)列表with open('switch_list.txt') as f:    switch_list = f.read().splitlines()# 遍歷交換機(jī)列表,備份配置文件for switch_ip in switch_list:    print(f'正在備份交換機(jī) {switch_ip} 的配置文件...')        # 建立SSH連接    ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10)    # 發(fā)送命令    ssh.exec_command('system-view')    time.sleep(1)    ssh.exec_command('save backup.cfg')    # 等待備份完成    time.sleep(5)    # 關(guān)閉SSH連接    ssh.close()    # 保存?zhèn)浞菸募?   filename = f'{switch_ip}.cfg'    os.system(f'tftp -g -r backup.cfg 10.0.0.1')    os.rename('backup.cfg', filename)    print(f'交換機(jī) {switch_ip} 的配置文件備份完成,保存為 {filename}。')

三、思科

import paramikoimport timeimport os# 創(chuàng)建SSH客戶端對(duì)象ssh = paramiko.SSHClient()# 自動(dòng)添加主機(jī)密鑰ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH連接信息username = 'username' # SSH用戶名password = 'password' # SSH密碼port = 22 # SSH端口號(hào)# 讀取交換機(jī)列表with open('switch_list.txt') as f: switch_list = f.read().splitlines()# 遍歷交換機(jī)列表,備份配置文件for switch_ip in switch_list: print(f'正在備份交換機(jī) {switch_ip} 的配置文件...') # 建立SSH連接 ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10) # 發(fā)送命令 ssh.exec_command('enable') time.sleep(1) ssh.exec_command('terminal length 0') time.sleep(1) ssh.exec_command('show running-config') time.sleep(5) # 獲取輸出結(jié)果 output = ssh.recv(65535).decode('ascii') # 關(guān)閉SSH連接 ssh.close() # 保存?zhèn)浞菸募? filename = f'{switch_ip}.cfg' with open(filename, 'w') as f: f.write(output) print(f'交換機(jī) {switch_ip} 的配置文件備份完成,保存為 {filename}。')

四、銳捷

import telnetlibimport timeimport os# 配置Telnet連接信息username = b'username\n'  # Telnet用戶名,注意要使用字節(jié)串password = b'password\n'  # Telnet密碼,注意要使用字節(jié)串port = 23  # Telnet端口號(hào)# 讀取交換機(jī)列表with open('switch_list.txt') as f:    switch_list = f.read().splitlines()# 遍歷交換機(jī)列表,備份配置文件for switch_ip in switch_list:    print(f'正在備份交換機(jī) {switch_ip} 的配置文件...')    # 建立Telnet連接    tn = telnetlib.Telnet(switch_ip, port=port, timeout=10)    # 登錄    tn.read_until(b'>>User name:', timeout=5)    tn.write(username)    tn.read_until(b'>>User password:', timeout=5)    tn.write(password)    # 發(fā)送命令    tn.write(b'enable\n')    tn.write(password)    tn.write(b'display current-configuration\n')    time.sleep(5)    # 獲取輸出結(jié)果    output = tn.read_very_eager().decode('gbk')    # 關(guān)閉Telnet連接    tn.write(b'quit\n')    tn.close()    # 保存?zhèn)浞菸募?   filename = f'{switch_ip}.cfg'    with open(filename, 'w') as f:        f.write(output)    print(f'交換機(jī) {switch_ip} 的配置文件備份完成,保存為 {filename}。')

五、Juniper

import paramikoimport timeimport os# 創(chuàng)建SSH客戶端對(duì)象ssh = paramiko.SSHClient()# 自動(dòng)添加主機(jī)密鑰ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 配置SSH連接信息username = 'username' # SSH用戶名password = 'password' # SSH密碼port = 22 # SSH端口號(hào)# 讀取交換機(jī)列表with open('switch_list.txt') as f: switch_list = f.read().splitlines()# 遍歷交換機(jī)列表,備份配置文件for switch_ip in switch_list: print(f'正在備份交換機(jī) {switch_ip} 的配置文件...') # 建立SSH連接 ssh.connect(switch_ip, port=port, username=username, password=password, timeout=10) # 發(fā)送命令 ssh.exec_command('configure exclusive') time.sleep(1) ssh.exec_command('show') time.sleep(5) # 獲取輸出結(jié)果 output = ssh.recv(65535).decode('ascii') # 關(guān)閉SSH連接 ssh.close() # 保存?zhèn)浞菸募? filename = f'{switch_ip}.cfg' with open(filename, 'w') as f: f.write(output) print(f'交換機(jī) {switch_ip} 的配置文件備份完成,保存為 {filename}。')

六、腳本說明

以上腳本使用了Paramiko庫來實(shí)現(xiàn)SSH連接和命令執(zhí)行,使用了os庫來進(jìn)行文件操作。在使用腳本前,請(qǐng)確保已經(jīng)安裝好Paramiko庫并且已經(jīng)將需要備份的交換機(jī)的IP地址列表保存在名為switch_list.txt的文件中,每行一個(gè)IP地址。另外,需要將腳本中的username和password替換成實(shí)際的值。注意,Juniper交換機(jī)的備份命令與其他廠商的命令略有不同。

另外,由于華為和H3C設(shè)備的配置命令幾乎一致,思科和銳捷設(shè)備的配置命令也幾乎一致,所以寫出來的腳本也差不多一樣,大家在練習(xí)或者使用的時(shí)候注意一下就好。

總結(jié)

本文給大家介紹了五個(gè)廠商(華為、H3C、思科、銳捷、Juniper)批量備份交換機(jī)配置的Python腳本,希望對(duì)您有所幫助!如果您還想了解或者學(xué)習(xí)更多針對(duì)網(wǎng)絡(luò)設(shè)備配置的Python腳本,歡迎在下方評(píng)論區(qū)留言!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多