# -*- coding: utf-8 -*-
import ops
import sys
import os
import re
import time
def ops_condition (ops):
value, time1 = ops.timer.relative("timer1", 5)
return 0
def ops_execute (ops):
value, descri_str = ops.syslog("backup_cfg begin.", "warning", "syslog")
handle, err_desp = ops.cli.open()
curtime = time.strftime("%Y%m%d%H%M%S", time.localtime())
result, n11, n21 = ops.cli.execute(handle,"dis cur")
a = re.search(r'(?<=sysname ).*(?=\r\n)',result)
if a:
sysname = a.group()
result, n11, n21 = ops.cli.execute(handle,"dis startup")
a = re.search(r'(?<=Startup saved-configuration file:).*',result)
if a:
cfgfile = a.group()
cfgfile = cfgfile.strip()
devcfg = cfgfile.split('/')
devcfg1 = devcfg[1]
devcfgparts = devcfg1.split('.')
cmd = "tftp 192.168.1.3 put " + devcfg1 + " " + sysname + curtime + "." + devcfgparts[1]
value, descri_str = ops.syslog(sysname, "warning", "syslog")
result, n11, n21 = ops.cli.execute(handle,cmd)
time.sleep(1)
result = ops.cli.close(handle)
return 0
脚本准备好以后,上传到交换机的flash:目录,至于怎么上传到交换机,相信大多数网络工程师都会,不会的可以到网上搜一下相关的文章,脚本上传好以后dir一下,看看刚才上传的文件是否存在,这里可以看到文件已上传好
接下来在交换机上运行以下命令安装python脚本,配置python脚本助手:
ops install file backup_cfg.py
sys
ops
script-assistant python backup_cfg.py