华为交换机定时保存配置

使用python脚本每天下午6点保存一次华为交换机配置

python脚本:

#!/bin/python
import paramiko
import time

# 定义多个设备的IP地址列表
username = 'user'
hosts = ['192.168.0.2']
password = 'password'

# 创建一个函数,用于连接到主机并执行命令
def execute_commands(host, username, password):
    try:
        # 创建 SSH 连接
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # 连接主机
        ssh.connect(host, username=username, password=password)

        # 执行 save 命令
        stdin, stdout, stderr = ssh.exec_command('save')

        # 等待命令执行完成并发送 'y'
        time.sleep(3)
        stdin.write('y\n')
        stdin.flush()

        # 输出命令执行结果
        print(f"Output from {host} (save):")
        print(stdout.read().decode('utf-8'))


        # 关闭 SSH 连接
        ssh.close()
    except paramiko.AuthenticationException:
        print(f"Authentication failed when connecting to {host}")
    except paramiko.SSHException as ssh_ex:
        print(f"Unable to establish SSH connection to {host}: {ssh_ex}")
    except Exception as ex:
        print(f"Error: {ex}")


# 主函数
def main():
    for host in hosts:
        execute_commands(host, username, password)

if __name__ == "__main__":
    main()

配置定时任务,下午六点执行脚本

crontab -e
0 18 * * * "python3 /root/huawei.py"

华为交换机可以通过配置来实现定时备份配置的功能。首先,需要设置保存配置的时间间隔和延迟保存的时间。可以使用命令"set save-configuration interval"来设置备份配置的时间间隔,默认为30分钟。使用命令"set save-configuration delay"来设置配置发生变化后延迟保存的时间。例如,"set save-configuration delay 1"表示配置发生变化后1分钟后自动保存配置。 接下来,需要设置备份配置的服务器信息。可以使用命令"set save-configuration backup-to-server"来设置备份配置的服务器。需要指定服务器的IP地址、传输类型(如FTP、SFTP或TFTP)、用户名和密码。例如,"set save-configuration backup-to-server server 10.0.0.1 transport-type ftp user test password test"表示将配置保存到FTP服务器10.0.0.1,使用用户名test和密码test进行认证。 通过以上配置华为交换机就可以实现定时备份配置的功能了。每隔设定的时间间隔,交换机会自动保存新的配置,并将其上传到指定的服务器上。同时,配置发生变化后,会在设定的延迟时间后自动保存新的配置并上传到服务器。 #### 引用[.reference_title] - *1* [华为交换机---自动备份配置到指定ftp服务器](https://blog.csdn.net/qq_42906357/article/details/127237845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值