交換機配置自動備份
使用 python3
多線程 ssh
批量登錄交換機並獲取到交換機配置,自動刪除 30
天以前的配置,將命令配置到 crontab
實現每天備份,支持新華三,華爲,銳捷,思科交換機
源碼:https://github.com/xin053/switchbackup
安裝依賴
# python3 環境
# ubuntu
apt install -y python3-pip
# centos
yum install -y python3-pip
# pip3 更新並設置源
pip3 install pip --upgrade -i https://mirrors.aliyun.com/pypi/simple/
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
cd /opt
git clone https://github.com/xin053/switchbackup
cd switchbackup
pip3 install -r requirements.txt
修改配置文件 hosts.yaml
按照以下格式, 注意縮進, yaml
文件對縮進要求很嚴格
支持的 type
有 h3c, huawei, ruijie, cisco
# 備份文件保存路徑
backup_path: '/home/xin053/swConfigBackup'
# 備份文件保存時長, 單位: 天
keep_time: 30
hosts:
- name: xxxH3C6800
type: h3c
ip: xxx.xxx.xxx.xxx
port: 22
username: xxx
password: xxx
- name: xxxCE6810-01
type: huawei
ip: xxx.xxx.xxx.xxx
port: 22
username: xxx
password: xxx
使用
# 命令格式
python3 switchbackup.py [ip] [ip] ...
cd /opt/switchbackup
# 備份配置文件中的全部交換機
python3 switchbackup.py
# 備份配置文件中指定交換機
python3 switchbackup.py xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx
配置 crontab
每天凌晨執行備份:
0 0 * * * cd /opt/switchbackup && python3 switchbackup.py