python3以ftp方式備份華爲交換機配置文件

客戶這裏,有很多華爲S系列交換機,基本時都是2700,5700系列。數量很多,原來都是手工登陸備份,費時,費力。後來想用python腳本備份交換機配置文件。
思路:
1、華爲交換機的配置文件都是以vrpcfg.zip文件方式保存在交換機內存中
2、華爲的交換機都支持ftp服務器
3、使用python3腳本批量備份保存在windows主機指定目錄
4、有些設備可能故障等原因,無法進行備份,需要記錄失敗日誌

前提條件,windows上已經安裝好python3.6,配置好環境變量,腳本如下:

#! env python
#coding=utf-8

#ver2.0 
#使用ftp方式備份華爲交換機配置文件
#python3版本
from ftplib import FTP
import time
import os
import sys

dic = {
    'tongjiju': ['10.42.243.1',
                 '10.42.243.2',
                 '10.42.243.3',
                 '10.42.243.4',
                 '10.42.243.5',
                 '10.42.243.6',
                 '10.42.243.7',
                 '10.42.243.8',
                 '10.42.243.9',
                 '10.42.243.10',
                 '10.42.243.11',
                 '10.42.243.12',
                 '10.42.243.13',
                 '10.42.243.14',
                 '10.42.243.22',
                 '10.42.243.23',
                 '10.42.243.24',
                 '10.42.243.27',
                 '10.42.243.31',
                 '10.42.243.32',
                 '10.42.243.34',
                 '10.42.243.50'],
    'caizheng': ['192.168.10.11',
                 '192.168.10.17',
                 '192.168.10.16',
                 '192.168.10.18',
                 '192.168.10.19',
                 '192.168.10.20',
                 '192.168.10.22',
                 '192.168.10.23',
                 '192.168.10.24',
                 '192.168.10.26',
                 '192.168.10.27',
                 '192.168.10.28',
                 '192.168.10.30',
                 '192.168.10.31',
                 '192.168.10.32',
                 '192.168.10.33',
                 '192.168.10.34',
                 '192.168.10.35',
                 '192.168.10.36',
                 '192.168.10.37'],
    'jianhangjiankong': ['172.31.212.3',
                         '172.31.212.4',
                         '172.31.222.2',
                         '172.31.223.2',
                         '172.31.224.2',
                         '172.31.225.2',
                         '172.31.226.2',
                         '172.31.212.5',
                         '172.31.212.30'],
    'jingzhouQinQ': ['192.168.200.10',
                     '192.168.200.11',
                     '192.168.200.12',
                     '192.168.200.13',
                     '192.168.200.14',
                     '192.168.200.15',
                     '192.168.200.16'],

#jingzhoushilian': ['172.31.221.67',
#‘172.31.221.68',
#'172.31.221.69',
#'172.31.221.70',
#'172.31.221.71',
#'172.31.221.72'],

    'jiancewang': ['172.31.218.2'],
    'guoganjiance': ['172.31.220.2', '172.31.220.3'],
    'tongjiyiyuan': ['172.31.219.2'],
    'tushuguan': ['172.31.211.2'],
    'zhuanwang': ['172.31.214.2'],
    'dishui': ['172.31.216.12',
               '172.31.216.2',
               '172.31.216.11',
               '172.31.216.12',
               '172.31.216.13',
               '172.31.216.14',
               '172.31.216.38',
               '172.31.216.23',
               '172.31.216.24',
               '172.31.216.40',
               '172.31.216.39',
               '172.31.216.37',
               '172.31.216.34',
               '172.31.216.33',
               '172.31.216.19',
               '172.31.216.20',
               '172.31.216.35',
               '172.31.216.36',
               '172.31.216.9'],
    'jifang': ['192.168.1.7',
               '192.168.1.8',
               '192.168.1.11',
               '192.168.1.13',
               '172.31.205.29',
               '192.168.1.5',
               '192.168.1.6',
               '192.168.1.12']
}

def save(hosts, ftp):
    url = 'G:\設備資料備份\{}'.format(hosts)
    Today = time.strftime("%Y-%m-%d", time.localtime())
    clock = time.strftime("%H:%M:%S", time.localtime())
    ftp_error_log = 'G:\設備資料備份\備份日誌\{}.txt'.format(Today)
    if not os.path.exists(url):
        os.mkdir(url)
    for host in dic[hosts]:
        os.chdir(url)
        if not os.path.exists(host):
            os.mkdir(host)
            os.chdir(host)
        else:
            os.chdir(host)
        try:
            ftp.connect(host=host, port=21)
            ftp.login(user='admin', passwd='xxxxxx')
            bufsize = 1024
            filename = "{}.zip".format(Today)
            file_handle = open(filename, "wb").write
            ftp.retrbinary("RETR vrpcfg.zip", file_handle, bufsize)
            print("login " + host)
            print(ftp.getwelcome())
            print(host + " ftp down ok")
        except Exception as e:
            print('{} is loss , msg:-{},time is {}'.format(host, e, clock))
            print('{} is loss , msg:-{},time is {}'.format(host, e, clock),file=open(ftp_error_log,'a'))  #保存錯誤日誌

def main():
    ftp = FTP()
ftp.set_debuglevel(2)
    # 0主動模式 1 #被動模式
    ftp.set_pasv(0)
    for hosts in dic.keys():
        save(hosts, ftp)
        #關閉調試模式
    #ftp.set_debuglevel(0)
    ftp.quit()
    ftp.close()
    #ftp_error_log.close()

if __name__ == '__main__':
    main()

運行時,將信息輸出在終端
python3以ftp方式備份華爲交換機配置文件
運行完成後,可以將錯誤信息以文本的方式保存
python3以ftp方式備份華爲交換機配置文件

然後查看保存的交換機配置文件
python3以ftp方式備份華爲交換機配置文件

在windows上開啓計劃任務,定期執行

需要使用此腳本時,注意更換字典中的主機ip地址,用戶密碼,文件路徑等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章