使用python telnetlib批量備份交換機配置的方法
使用了telnetlib模塊,首先登錄到交換機,列出并獲取配置文件的名稱,然后通過tftp協(xié)議將配置文件傳輸?shù)轿募?wù)器上,為避免配置文件覆蓋,將備份的配置文件名稱統(tǒng)一加入日期以作區(qū)分。
1. 登錄方式和口令有好幾種,比較懶惰,通過不同列表以做區(qū)分,如果每個交換機口令都不相同的話,就需要額外處理了。
2. 交換機的配置文件也有多種類型,也是通過列表進行區(qū)分。
3. 有些交換機支持ftp和sftp,但測試發(fā)現(xiàn)有些雖然有相應(yīng)的客戶端命令,但傳輸總有問題。也不能將每個交換機都配置為ftp服務(wù)器,不安全也不方便。最后采用tftp解決。tftp比較簡單,沒有辦法創(chuàng)建目錄以區(qū)分不同日期的備份。好在配置文件已經(jīng)加入了日期做區(qū)分,馬馬虎虎可以運行了。
import telnetlib,sys
from datetime import date
today=date.today()
print(today)
ipaddrset1=['192.168.1.19','192.168.1.29','192.168.1.59']
ipaddrset2=['192.168.1.39','192.168.1.49','192.168.1.69','192.168.1.56','192.168.1.6','192.168.1.9','192.168.1.24',
'192.168.1.72','192.168.1.73','192.168.1.74','192.168.1.75','192.168.1.76','192.168.1.41','192.168.1.16','192.168.1.32',]
ipaddrset3=['192.168.1.51','192.168.1.52','192.168.1.53','192.168.1.54','192.168.1.55',
'192.168.1.15','192.168.1.16','192.168.1.22','192.168.1.23','192.168.1.25','192.168.1.26','192.168.1.27',
'192.168.1.28','192.168.1.7']
hostname='192.168.8.201'
tn=telnetlib.Telnet(hostname)
print(tn.read_until(b'Username:').decode('ascii'))
tn.write(b'**********\n')
print(tn.read_until(b'Password:').decode('ascii'))
tn.write(b'************\n')
print(tn.read_until(b'>').decode('ascii'))
for ipaddr in ipaddrset1:
telnet_dest="telnet "+ipaddr
tn.write(telnet_dest.encode('ascii')+b'\n')
tn.read_until(b'Password:').decode('ascii')
tn.write(b'**********\n')
tn.read_until(b'>').decode('ascii')
tn.write(b'dir\n')
tn.read_until(b'>').decode('ascii')
fn=str(today)+"_"+str(ipaddr)+"_vrpcfg.zip \n"
cmdli="tftp 192.168.5.33 put vrpcfg.zip " +str(fn)
tn.write(cmdli.ede('ascii'))
tmp=tn.read_until(b'>').decode('ascii')
if "successfully" in tmp:
print(str(ipaddr)+" backup successfully!")
else:
print(str(ipaddr)+" backup NOT successfully!")
tn.write(b'quit\n')
tn.read_until(b'>')
for ipaddr in ipaddrset2:
telnet_dest="telnet "+ipaddr
tn.write(telnet_dest.encode('ascii')+b'\n')
tn.read_until(b'Password:').decode('ascii')
tn.write(b'**********\n')
tn.read_until(b'>').decode('ascii')
tn.write(b'dir\n')
tn.read_until(b'>').decode('ascii')
fn=str(today)+"_"+str(ipaddr)+"_startup.cfg \n"
cmdli="tftp 192.168.5.33 put startup.cfg " +str(fn)
tn.write(cmdli.encode('ascii'))
tmp=tn.read_until(b'>').decode('ascii')
if "successfully" in tmp:
print(str(ipaddr)+" backup successfully!")
else:
print(str(ipaddr)+" backup NOT successfully!")
tn.write(b'quit\n')
tn.read_until(b'>')
for ipaddr in ipaddrset3:
telnet_dest="telnet "+ipaddr
tn.write(telnet_dest.encode('ascii')+b'\n')
tn.read_until(b'Password:').decode('ascii')
tn.write(b'************\n')
tn.read_until(b'>').decode('ascii')
tn.write(b'dir\n')
tn.read_until(b'>').decode('ascii')
fn=str(today)+"_"+str(ipaddr)+"_startup.cfg \n"
cmdli="tftp 192.168.5.33 put startup.cfg " +str(fn)
tn.write(cmdli.encode('ascii'))
tmp=tn.read_until(b'>').decode('ascii')
if "successfully" in tmp:
print(str(ipaddr)+" backup successfully!")
else:
print(str(ipaddr)+" backup NOT successfully!")
tn.write(b'quit\n')
tn.read_until(b'>')
tn.write(b'exit\n')
tn.close()
以上這篇使用python telnetlib批量備份交換機配置的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3+Appium實現(xiàn)多臺移動設(shè)備操作的方法
這篇文章主要介紹了Python3+Appium實現(xiàn)多臺移動設(shè)備操作的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python中print和return的作用及區(qū)別解析
print的作用是輸出數(shù)據(jù)到控制端,就是打印在你能看到的界面上。這篇文章給大家介紹Python中print和return的作用及區(qū)別解析,感興趣的朋友跟隨小編一起看看吧2019-05-05
Python中matplotlib庫安裝失敗的經(jīng)驗總結(jié)(附pycharm配置anaconda)
最近根據(jù)領(lǐng)導(dǎo)布置的學(xué)習(xí)任務(wù),開始學(xué)習(xí)python中的matplotlib,朋友告訴我這個很簡單,然而剛踏入安裝的門檻,就遇到了安裝不成功的問題,下面這篇文章主要給大家介紹了關(guān)于Python中matplotlib庫安裝失敗的經(jīng)驗總結(jié),需要的朋友可以參考下2022-08-08
Python實現(xiàn)字符串模糊匹配的兩種實現(xiàn)方法
本文主要介紹了Python實現(xiàn)字符串模糊匹配的兩種實現(xiàn)方法,Python中通過re.search()方法實現(xiàn),對于首位起始的內(nèi)容匹配,也可通過re.match()方法實現(xiàn),感興趣的可以了解一下2023-11-11
Django中URLconf和include()的協(xié)同工作方法
這篇文章主要介紹了Django中URLconf和include()的協(xié)同工作方法,Django是Python眾人氣框架中最著名的一個,需要的朋友可以參考下2015-07-07

