Giter Club home page Giter Club logo

routeros-script's Introduction

自用 RouterOS 脚本库

[toc]

Module.src 加载其他依赖脚本模块

为何要设计那么复杂的模块?

我在编写和调试脚本的时候,经常遇到这个功能和其他脚本功能重复的情况,例如脚本日志控制 logger,当我修改它的时候,其他脚本依赖又需要重新修改。

为了解决这个需求,我尝试使用 Mikrotik 推荐的从 A 脚本中引入为 B 脚本。

https://wiki.mikrotik.com/wiki/Manual:Scripting

添加脚本

#add script
 /system script add name=myScript source=":put \"Hello $myVar !\""

在另一个脚本里引入它

:global myFunc [:parse [/system script get myScript source]]
$myFunc myVar=world

output:
Hello world !

但是这种方案有个致命缺点就是一跑脚本,/system script environment 里面就会有一大堆全局变量,脚本少还行,如果多的情况下会遇到例如:

  • 不同脚本的全局环境变量重名覆盖导致异常
  • 过多的环境变量引入内存中,导致一些小内存机器因为加载脚本太多,内存紧张
  • 有些脚本环境变量不会回收

可能有人会说,你可以把脚本加载到 local 上,就不会有问题了啊。

对!的确是可以的,但 local 的作用域仅限于当前的程序块 [],如果 Func::A 需要调用 Func::B 呢么你 local 就不行了,需要在把 Func::B 定义为全局变量,在调用结束后还得回收这个 Func::B。

为了解决这个问题,所有模块脚本最终都会将自己作用域的方法放入 Array 内,最终将 Array return 到当前作用域,实现加载任务。

loadModule 介绍

引入脚本模块,脚本最后必须将函数代码放入 array 中,然后通过 return array 传递给当前脚本使用。

模块加载 loadModule

使用方法:

:local logger ([ $loadModule logger init=({"logTag"=$logTag;"level"=$logLevel;}) ]->"logger")

参数:

  • init: 作为数组传入 loadModule ,如模块发现加载模块有 init 方法,则调用它,范例是初始化当前 logger

未完待续.....

routeros-script's People

Contributors

neroxps avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

routeros-script's Issues

FAKE-IP 分流方案探讨

大佬你好,我现在就是使用的 FAKE-IP 分流方案,当 dst 是 fake-ip 时我就打标记,但现在出现个问题,就是回环了,从路由跟踪来看,不停的 ROS - OP - ROS - OP ... 的循环,不知道你的解决方案是什么

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.