最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

如何寫批量備份交換機(jī)配置的Python腳本?今日文章安排5個(gè)廠商的,牛叉!

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

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

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

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

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

下面讓我們直接開始!

一、華為

import?paramiko
import?time
import?os

#?創(chuàng)建SSH客戶端對象
ssh?=?paramiko.SSHClient()

#?自動(dòng)添加主機(jī)密鑰
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#?配置SSH連接信息
username?=?'username'??#?SSH用戶名
password?=?'password'??#?SSH密碼
port?=?22??#?SSH端口號

#?讀取交換機(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)浞菸募?/span>
????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?paramiko
import?time
import?os

#?創(chuàng)建SSH客戶端對象
ssh?=?paramiko.SSHClient()

#?自動(dòng)添加主機(jī)密鑰
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#?配置SSH連接信息
username?=?'username'??#?SSH用戶名
password?=?'password'??#?SSH密碼
port?=?22??#?SSH端口號

#?讀取交換機(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)浞菸募?/span>
????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?paramiko
import?time
import?os

#?創(chuàng)建SSH客戶端對象
ssh?=?paramiko.SSHClient()

#?自動(dòng)添加主機(jī)密鑰
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#?配置SSH連接信息
username?=?'username'??#?SSH用戶名
password?=?'password'??#?SSH密碼
port?=?22??#?SSH端口號

#?讀取交換機(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)浞菸募?/span>
????filename?=?f'{switch_ip}.cfg'
????with?open(filename,?'w')?as?f:
????????f.write(output)

????print(f'交換機(jī)?{switch_ip}?的配置文件備份完成,保存為?{filename}。')

四、銳捷

import?telnetlib
import?time
import?os

#?配置Telnet連接信息
username?=?b'username\n'??#?Telnet用戶名,注意要使用字節(jié)串
password?=?b'password\n'??#?Telnet密碼,注意要使用字節(jié)串
port?=?23??#?Telnet端口號

#?讀取交換機(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)浞菸募?/span>
????filename?=?f'{switch_ip}.cfg'
????with?open(filename,?'w')?as?f:
????????f.write(output)

????print(f'交換機(jī)?{switch_ip}?的配置文件備份完成,保存為?{filename}。')

五、Juniper

import?paramiko
import?time
import?os

#?創(chuàng)建SSH客戶端對象
ssh?=?paramiko.SSHClient()

#?自動(dòng)添加主機(jī)密鑰
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#?配置SSH連接信息
username?=?'username'??#?SSH用戶名
password?=?'password'??#?SSH密碼
port?=?22??#?SSH端口號

#?讀取交換機(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)浞菸募?/span>
????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)行文件操作。在使用腳本前,請確保已經(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腳本,希望對您有所幫助!如果您還想了解或者學(xué)習(xí)更多針對網(wǎng)絡(luò)設(shè)備配置的Python腳本,歡迎在下方評論區(qū)留言!


如何寫批量備份交換機(jī)配置的Python腳本?今日文章安排5個(gè)廠商的,牛叉!的評論 (共 條)

分享到微博請遵守國家法律
高唐县| 武义县| 宝应县| 钦州市| 金乡县| 怀来县| 南汇区| 象州县| 和林格尔县| 灌南县| 北安市| 隆德县| 论坛| 南部县| 综艺| 疏附县| 南江县| 中江县| 汕头市| 深水埗区| 济宁市| 博罗县| 孟连| 丰宁| 东丽区| 亳州市| 乌拉特中旗| 独山县| 秭归县| 青铜峡市| 罗江县| 宁德市| 北辰区| 布尔津县| 丽江市| 内黄县| 贞丰县| 黎川县| 昆山市| 沾益县| 潜山县|