zoukankan      html  css  js  c++  java
  • ROS SCript课程系列3

    官方脚本

    https://wiki.mikrotik.com/wiki/Scripts

    RouterOS SCript课程系列

    第65课程 RouterOS Script语言

    RouterOS script语言是RouterOS内置的,功能强大,语法灵活,脚本经常与命令结合,通过脚本,可以实现自动化操作,提高工作效率等,

    RouterOS脚本语言非常的强大,是整个RouterOS的核心,是精通RouterOS的必由之路。

    熟悉Script脚本的前提是熟悉RouterOS的CLI命令行操作。我们将利用几节课的时间来进行学习,那现在就开始吧。

    10、命令

    这一节课,我们来学习命令,在RouterOS中,脚本都是以命令作为基础的。

    全局命令

    全局命令都应以“:”标记开头,否则将被视为变量。


    简单的符号也是命令,如下:/ ,..

    可以用?获取可用的命令即其简要的帮助信息,注意颜色


    beep --
    blink --
    caps-man --
    certificate -- Certificate management
    console --
    delay -- does nothing for a while *#delay 5s
    disk --
    do -- executes command
    environment -- list of all variables * #:environment print;
    error -- make error value * error ("aaa")
    execute -- run script as separate console job *
    file -- Local router file storage.
    find -- Find items by value * #:put [:find "abc" "a" -1];
    for -- executes command for a range of integer values
    foreach -- executes command for every element in a list
    global -- set value global variable
    if -- executes command if condition is true
    import -- *
    interface -- Interface configuration
    ip --
    ipv6 --
    len -- return number of elements in value * #:put [:len "length=8"];
    local -- set value of local variable
    log -- System logs * :log info "Hello from script";
    metarouter --
    nothing -- do nothing and return nothing
    parse -- build command from text
    partitions --
    password -- Change password
    pick -- return range of string characters or array values * #:put [:pick "abcde" 1 3]
    ping -- Send ICMP Echo packets
    port -- Serial ports
    ppp -- Point to Point Protocol
    put -- prints argument on the screen *
    queue -- Bandwidth management
    quit -- Quit console
    radius -- Radius client settings
    redo -- Redo previously undone action
    resolve -- perform a dns lookup of domain name *#:put [:resolve "www.mikrotik.com"];
    return -- return value from function *
    routing --
    set -- Change item properties *
    snmp -- SNMP settings
    special-login -- Special login users
    system --
    terminal -- commands related to terminal handling
    time -- returns time taken by command to execute * #:put [:time {:for i from=1 to=10 do={ :delay 100ms }}];
    toarray -- convert argument to array value
    tobool -- convert argument to truth value
    toid -- convert argument to internal number value
    toip -- convert argument to IP address value
    toip6 -- convert argument to IPv6 address value
    tonum -- convert argument to integer number value
    tool -- Diagnostics tools
    tostr -- convert argument to string value
    totime -- convert argument to time interval value
    typeof -- return type of value * #:put [:typeof 4];
    undo -- Undo previous action
    user -- User management
    while -- executes command while condition is true
    export -- Print or save an export script that can be used to restore configuration *

    命令的操作 选项:

    add add <param>=<value>..<param>=<value> add new item

    remove remove <id> remove selected item

    enable enable <id> enable selected item

    disable disable <id> disable selected item

    set set <id> <param>=<value>..<param>=<value> change selected items parameter, more than one parameter can be specified at the time. Parameter can be unset by specifying '!' before parameter.

    Example:

    /ip firewall filter add chain=input action=accept protocol=tcp port=123 nth=4,2

    print
    set 0 !port chain=output !nth protocol=udp

    get get <id> <param>=<value> get selected items parameter value
    print print <param><param>=[<value>] print menu items. Output depends on print parameters specified. Most common print parameters are described here
    export export [file=<value>]
    edit edit <id> <param> edit selected items property in built-in text editor

    find find <expression> Returns list of internal numbers for items that are matched by given expression. For example: :put [/interface find name~"ether"]


    print :

    print as-value 将输出作为数组保存 :put [/ip address print as-value]

    print file= 输出到文件

    follow: 跟随 打印所有当前条目并跟踪新条目,直到按ctrl-c为止,这在查看日志条目时非常有用 /log print follow

    from: #/user print from=admin
    value-list

    where :/ip route print where interface="ether1"


    补充一下数组的的操作:

    a[key]=value

    {:local a { "aX"=1 ; ay=2 }; :put ($a->"aX")}

    遍历:

    键和值

    :foreach k,v in={2; "aX"=1 ; y=2; 5} do={:put ("$k=$v")}

    只有一个参数,返回的是值value非key

    :foreach k in={5; "aX"=1 ; y=2; 3} do={:put ("$k")}

    注意:

    如果数组元素有key,则这些元素将顺序排序,没有key的元素将在具有key的元素之前移动,并且顺序不会变

    :global a {x=10; y=20}
    :set ($a->"x") 50
    #set($ arrayVariable-> $arrayIndex)$ newValue

    :environment print
    a={x=50; y=20}


    没有key的数组如何修改元素?思考!

    :global month January,February,March,April,May,June,July,August,September,October,November,December

    仿写函数


    ip:192.168.100.100

    #https://www.paperstreetonline.com/2014/06/06/mikrotik-scripting-split-an-ip-address-into-an-array/

    # Usage: [$returnOctet <ip> <octet number (0-4)>]
    # Input an IP Address and 0-3 argument to return a specific octet number or input a 4 to return all octets as an array

    :global returnOctet do={
    :if ($1="") do={ :error "You did not specify an IP Address."; }
    :if ($2="") do={ :error "You did not specify an octet to return."; }
    :if (($2>"4") || ($2<"0")) do={ :error "Octet argument out of range."; }

    :local decimalPos "0";
    :local octet1;
    :local octet2;
    :local octet3;
    :local octet4;

    :local octetArray;
    :set decimalPos [:find $1 "."];
    :set octet1 [:pick $1 0 $decimalPos];
    :set decimalPos ($decimalPos+1);
    :set octet2 [:pick $1 $decimalPos [:find $1 "." $decimalPos]];
    :set decimalPos ([:find $1 "." $decimalPos]+1);
    :set octet3 [:pick $1 $decimalPos [:find $1 "." $decimalPos]];
    :set decimalPos ([:find $1 "." $decimalPos]+1);
    :set octet4 [:pick $1 $decimalPos [:len $1]];
    :set octetArray [:toarray "$octet1,$octet2,$octet3,$octet4"];

    :if (($octet1<"0" || $octet1>"255") || ($octet2<"0" || $octet2>"255") || ($octet3<"0" || $octet3>"255") || ($octet4<"0" || $octet4>"255")) do={ :error "Octet out of range."; }
    :if ($2="0") do={ :return $octet1; }
    :if ($2="1") do={ :return $octet2; }
    :if ($2="2") do={ :return $octet3; }
    :if ($2="3") do={ :return $octet4; }
    :if ($2="4") do={ :return $octetArray; }


    }


    #edit by 大玩家 qq:1247004718

    :global ipsplit do={

    :if ($1="") do={ :error "You did not specify an IP Address."; }
    :if ($2="") do={ :error "You did not specify an octet to return."; }
    :if (($2>"4") || ($2<"1")) do={ :error "Octet argument out of range."; }

    :local a1;
    :local a2;
    :local a3;
    :local a4;

    :local a11 ( $1 & 255.0.0.0 );
    :set a1 [:pick $a11 0 [:find $a11 "."]];

    :local a12 (( $1 & 0.255.0.0 )<<8);
    :set a12 [:pick $a12 0 [:find $a12 "."]];

    :local a13 (( $1 & 0.0.255.0 )<<16);
    :set a13 [:pick $a13 0 [:find $a13 "."]];

    :local a14 (( $1 & 0.0.0.255 )<<24);
    :set a14 [:pick $a14 0 [:find $a14 "."]];

    :if ($2="1") do={:return $a1}
    :if ($2="2") do={:return $a12}
    :if ($2="3") do={:return $a13}
    :if ($2="4") do={:return $a14}

    }

    https://wiki.mikrotik.com/wiki/Scripts

  • 相关阅读:
    使用IDEA模拟git命令使用的常见场景
    解决 No converter found for return value of type: class java.util.ArrayList
    Connections could not be acquired from the underlying database! ### The error may exist in mapper/BookMapper.xml
    There is no PasswordEncoder mapped for the id "null"
    The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone
    MySQL错误:2003-Can't connect to MySQL server on 'localhost'(10061 "unknown error")
    镜像源
    读书笔记 Week5 2018-4-5
    [第五周课后作业] 软件创新分析
    C#入门学习笔记
  • 原文地址:https://www.cnblogs.com/itfat/p/12929804.html
Copyright © 2011-2022 走看看