文档

脚本编写说明

更新时间:

脚本是一段Python代码,要求脚本里面定义好与模板名同名的函数,作为执行任务的入口函数。

代码限制

  • 禁用内置函数print,while。

  • 限制range使用,最大不超过1000。

  • import白名单列表:

    • re

    • json

    • time

    • datetime

    • IPy

    • hashlib

    • base64

    • uuid.uuid4

    • difflib

    • ipaddress

    • copy

    • random

    • math

    • string

  • 禁用下划线开头_命名的变量/函数。

内置函数说明

  • logger

    • logger是标准模块logging.Logger的一个实例。

    • 用户可以参考下⾯的⽅式输出日志,所有的日志都会记录在数据库中,可以在任务管理⻚⾯查询。

def logger_example():
    logger.error("an error message")
    logger.warn("a warning message")
    logger.info("an info message")
    logger.debug("a debug message")
  • login_device/device_login

    • login_device/device_login负责处理设备的登录登出,以及保证设备操作的串⾏化。

  • exec_cli

    • exec_cli负责向设备下发命令,只能用于login_device装饰的函数中。

    • 完整参数列表:

      • command: string|list,向设备下发的指令或指令列表。

      • timeout: int,默认65s,系统等待设备响应命令完成的超时时间,设备没能在指定时间内完成响应则会有Timeout异常抛出。

      • style: string,控制返回内容结构。

        • simple: 回显去除掉input command和终端提示符后剩余内容。

        • tuple: 将回显内容进⾏拆分,返回(command,output)的列表。

        • verbose: 默认值,将设备回显内容原样返回。

        • strict: boolean,默认值True,是否对回显内容作异常关键字判断。如果命中关键字,则会抛出异常SYSTEM_BUSY或BAD_DEVICE_RESPONSE,⽬前关键字列表包括:

          • System is busy

          • Permission denied

          • syntax error.

          • Unrecognized command

          • Invalid command

          • Invalid parameter

          • at '^' marker

          • Unknown command

          • at '^' position.

          • Incomplete command

          • unknown command

          • Operation fail

          • error: configuration database locked by

          • TCAM region is not configured

          • Invalid input

          • Request denied

          • Authorization denied

          • error: commit failed

          • Unavailable command

          • Cannot apply ACL

  • exec_script

    • exec_script用于调用其他模板。

def exec_script_example(target, cli):
    # 调用exec_cli_example的设备模板
    return exec_script('exec_cli_example', _target=target, cli=cli)
  • exec_script_async

    • exec_script_async用于异步调用模板。

def exec_script_async_example(target, cli):
    # 异步调用device_login_example的设备模板
    task =  exec_script('exec_cli_example', _target=target, cli=cli)
    # 获取异步任务的结果
    result = task.get()
    return result
  • current_device

    • current_device用于获取当前登录的设备。

@login_device
def get_device():
    device = current_device()
    device['ip']  # 设备IP
    device['hostname'] # 主机名 
    device['domain'] # 安全域
    device['space'] # 物理空间
    device['status'] # 状态
    device['role'] # 角色
    device['vendor'] # 厂商
    device['model'] # 型号
    device['sn'] # SN
    device['device_id'] # id
  • abort

    • abort用于终止任务执行。

    • 如下示例代码,运行时,任务会执行失败,返回为{"message": "xxx", "traceback": [[ "__shadow_{脚本MD5值}_abort_example_{版本ID}__",2,"abort_example",null]], "error_code": "OZ_USER_ABORT"}。

def abort_example():
    abort("xxx")
  • get_exc_code

    • 捕获异常时,可用该函数获取异常错误码。

  • get_traceback

    • 捕获异常时,可用该函数获取错误堆栈。

def exc_example():
    try:
        abort("xxx")
    except Exception as e:
        e.message #返回"xxx"
        get_exc_code(e) #返回"OZ_USER_ABORT"
        get_traceback() #返回[["__shadow_{脚本MD值}_{模板名}_{版本ID}__",
                        #     {异常发生代码行数},
                        #     {异常所在函数名},
                        #     null]]

输入参数说明

  • 设备模板默认第一个参数是_target,类型是string,可以是设备的id,设备的主机名或者IP。

输出参数说明

  • 主要用于变更方案步骤编辑中。

代码格式要求

  • 自动化模板代码格式要求。

    • 例如一个名为example_template的用户模板,代码必须全写在example_template这个函数里面,若有同级其他函数,引用时需要申明global。

    • re, json, time, datetime已经内置,可以不需要写import。

      • 正确示例

def func_a():
    pass

def example_template():
    import xxx
    global func_a
    
    def func_b():
        pass
    
    func_a()
    func_b()
  • 巡检模板代码格式要求。

    • 巡检模板代码里面只能允许有一个可执行函数。

  • 本页导读 (0)
文档反馈