你好,這里是網(wǎng)絡(luò)技術(shù)聯(lián)盟站。 在昨天的文章中,我們介紹了20個(gè)華為路由器常用的Python腳本:
文章末尾的評(píng)論區(qū)中,有小伙伴提出想要一下批量備份交換機(jī)配置的腳本: ![]() 既然沒有提出要哪個(gè)廠商,今天瑞哥就安排多廠商的腳本: ![]() 下面讓我們直接開始! 一、華為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('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}。') 四、銳捷
五、Juniperimport 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ū)留言! |
|