在网络设备管理中,定期备份设备配置是一项至关重要的任务。这样做不仅可以保障网络的稳定性和安全性,还能够在设备出现故障时快速恢复服务。对于华为设备的运维人员来说,利用Python脚本实现自动备份设备配置将会极大地简化工作流程,本文将介绍如何轻松搞定这一任务。
定期备份设备配置是网络管理中的基本操作之一。在日常运维中,设备的配置可能会因为误操作、设备故障或者安全事件而丢失或损坏,因此备份配置可以帮助我们快速恢复服务。此外,备份也是审计和故障诊断的重要依据,可以帮助我们了解设备配置的变更历史和恢复先前状态。
<X_T1_AGG1>system-view immediately
[X_T1_AGG1]sysname X_T1_AGG1
[X_T1_AGG1]interface Vlanif 1
[X_T1_AGG1-Vlanif1]ip add 192.168.56.10 24
[X_T1_AGG1-Vlanif1]q
[X_T1_AGG1]interface g1/0/0
[X_T1_AGG1-GE1/0/0]undo shutdown
ping
命令测试,如下图:[X_T1_AGG1-aaa]local-user dev_user password cipher Huawei@123
Info: A new user is added.
[X_T1_AGG1-aaa]local-user dev_user service-type ssh
[X_T1_AGG1-aaa]local-user dev_user level 3
[X_T1_AGG1-aaa]undo local-user policy security-enhance // 禁止第一次登录修改密码策略
[X_T1_AGG1-aaa]undo local-user policy password change // 禁止第一次登录修改密码策略
[X_T1_AGG1-aaa]q
[X_T1_AGG1]ssh user dev_user
Info: Succeeded in adding a new SSH user.
[X_T1_AGG1]ssh user dev_user authentication-type password
[X_T1_AGG1]ssh user dev_user service-type stelnet sftp
[X_T1_AGG1]stelnet server enable
Info: Succeeded in starting the STelnet server.
[X_T1_AGG1]sftp server enable
Info: Succeeded in starting the SFTP server.
[X_T1_AGG1]sftp server default-directory cfcard: #设置SFTP登录的根目录
[X_T1_AGG1]user-interface vty 0 4
[X_T1_AGG1-ui-vty0-4]authentication-mode aaa
[X_T1_AGG1-ui-vty0-4]protocol inbound all
[X_T1_AGG1-ui-vty0-4]user privilege level 3
首先,我们需要安装Python的SSH库,例如paramiko
,以便与设备建立SSH连接并执行命令。
pip install paramiko
Datacom
设备的Python
类,其中包含了一些方法来执行命令、下载配置文件等操作。from paramiko import SSHClient, AutoAddPolicy
import schedule
from datetime import datetime
from time import sleep
from loguru import logger
class Datacom():
def __init__(self, server, username, password):
self.server = server
self.username = username
self.password = password
self.client = self._get_client()
self.cli = self.client.invoke_shell()
self.cli.send('screen-length 0 temporary\n')
sleep(5)
self.cli.recv(999999)
def _get_client(self):
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy)
client.connect(self.server, username=self.username, password=self.password,look_for_keys=False)
return client
def command(self, cmd):
self.cli.send("{}\n".format(cmd))
sleep(5)
return self.cli.recv(999999).decode()
def download_config(self, target, config_path='/vrpcfg.cfg'):
logger.info("copy config file with sftp-------------------") # 打印提示信息
another_client = self._get_client() # 创建另一个SSH连接客户端
sftp = another_client.open_sftp() # 打开SFTP会话
sftp.get(config_path, target) # 下载配置文件到本地路径
another_client.close() # 关闭SSH连接
def close(self):
self.client.close()
❝❞
Datacom
这个类中的download_config
方法中,接收两个参数,target
是保存配置到本地那里,config_path
是网络设备配置文件的路径,通常通过dir
查看。
download_config
实现备份配置文件。def backup_conf(device_ip,username,password,device_name):
datacom = Datacom(device_ip,username,password) #实例化Datacom
last_downloadtime = datetime.now()
datacom.download_config('{}_{}_{}_{}.zip'.format(
last_downloadtime.year,
last_downloadtime.month,
last_downloadtime.day,
device_name
)) #调用download_config方法实现备份
logger.info("backup config success")
datacom.close()
backup_conf
方法。if __name__ == "__main__":
device_ip = "192.168.56.10" #
username = "dev_user"
password = "Huawei@123"
device_name = "X_T1_AGG1
schedule.every(5).minutes.do(backup_conf,device_ip,username,password,device_name)
while True:
schedule.run_pending()
❝上述的代码
❞every(5)
中的5表示是每隔5分钟执行一次。
将以上代码保存为backup_huawei_config.py
文件,然后在命令行中运行该脚本,即可实现自动备份华为设备配置。
python backup_huawei_config.py
执行上述的代码会出现如下的结果,如下图:
通过以上简单的Python脚本,我们可以轻松实现华为设备配置的自动备份。这样的自动化操作不仅能够提高运维效率,还能够减少手工操作的错误,确保了网络设备配置的安全性和一致性。让我们把更多的精力投入到网络优化和故障排除中,让网络管理变得更加高效和可靠!
联系客服