1. # -*- coding: utf-8 -*-
    2. import ops
    3. import sys
    4. import os
    5. import re
    6. import time
    7. def ops_condition (ops):
    8. value, time1 = ops.timer.relative("timer1", 5)
    9. return 0
    10. def ops_execute (ops):
    11. value, descri_str = ops.syslog("backup_cfg begin.", "warning", "syslog")
    12. handle, err_desp = ops.cli.open()
    13. curtime = time.strftime("%Y%m%d%H%M%S", time.localtime())
    14. result, n11, n21 = ops.cli.execute(handle,"dis cur")
    15. a = re.search(r'(?<=sysname ).*(?=\r\n)',result)
    16. if a:
    17. sysname = a.group()
    18. result, n11, n21 = ops.cli.execute(handle,"dis startup")
    19. a = re.search(r'(?<=Startup saved-configuration file:).*',result)
    20. if a:
    21. cfgfile = a.group()
    22. cfgfile = cfgfile.strip()
    23. devcfg = cfgfile.split('/')
    24. devcfg1 = devcfg[1]
    25. devcfgparts = devcfg1.split('.')
    26. cmd = "tftp 192.168.1.3 put " + devcfg1 + " " + sysname + curtime + "." + devcfgparts[1]
    27. value, descri_str = ops.syslog(sysname, "warning", "syslog")
    28. result, n11, n21 = ops.cli.execute(handle,cmd)
    29. time.sleep(1)
    30. result = ops.cli.close(handle)
    31. return 0

    脚本准备好以后,上传到交换机的flash:目录,至于怎么上传到交换机,相信大多数网络工程师都会,不会的可以到网上搜一下相关的文章,脚本上传好以后dir一下,看看刚才上传的文件是否存在,这里可以看到文件已上传好
    image.png
    接下来在交换机上运行以下命令安装python脚本,配置python脚本助手:

    1. ops install file backup_cfg.py
    2. sys
    3. ops
    4. script-assistant python backup_cfg.py