实验环境:
centos7
python3
pip3 install netmiko
1. python脚本
import time from netmiko import ConnectHandler now = time.strftime("%Y%m%d", time.localtime(time.time())) log_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ip_list = [ [‘SW-101‘,‘10.255.224.254‘], [‘SW-102‘,‘10.255.224.101‘], [‘SW-103‘,‘10.255.224.102‘], [‘SW-104‘,‘10.255.224.103‘], [‘SW-105‘,‘10.255.224.104‘], [‘SW-106‘,‘10.255.224.105‘], [‘SW-107‘,‘10.255.224.106‘], ] SW = { ‘device_type‘:‘hp_comware‘, ‘ip‘:‘‘, ‘username‘:‘admin‘, ‘password‘:‘password‘, } for ip_item in ip_list: SW[‘ip‘] = ip_item[1] connect = ConnectHandler(**SW) print(log_time + ‘ Successfully connected to ‘ + ip_item[0]) save_commands = ‘save\n‘ + ‘y\n‘ + ‘\n‘ + ‘y\n‘ output = connect.send_command(save_commands) backup_commands = ‘tftp 10.255.224.13 put startup.cfg ‘ + ip_item[0] + ‘/‘ + ip_item[0] + ‘-‘ + now +‘.cfg‘ result = connect.send_command(backup_commands)
2. vim /etc/crontab
#每天0点执行备份脚本00 00 * * * root /root/python/backup_sw/backup1.py >> /root/python/backup_sw/backup1.log
原文地址:https://www.cnblogs.com/starbaba/p/12217741.html
时间: 2024-11-05 12:09:04