ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ROS SCript课程系列3

2020-05-21 12:03:51  阅读:264  来源: 互联网

标签:do set return SCript decimalPos 课程 print ROS local


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

标签:do,set,return,SCript,decimalPos,课程,print,ROS,local
来源: https://www.cnblogs.com/itfat/p/12929804.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有