ros 腳本詳細解釋

因爲下面這個問題,我覺得腳本詳細解釋也許對大家有用處,決定把以前網絡時候找的資料發上來,希望對大家有用處

[apple@sgg-gatewy] > :put (([/ip firewall address-list get 0 address ] & 255.255
.255.0) . \/24)  
202.103.24.0/24
這樣使用put可以實現把
202.103.24.0
和/24
結合成一個整體
但是怎麼樣把這個新的值202.103.24.0/24賦給一個變量呢,set不能完成,誰有好的建議或者意見
我準備做的是通過***客戶端的訪問源地址,自動添加/ip route rule裏面的數據
現在可以自動把202.103.24.68這樣的具體ip自動添加,但是我想根據這個ip然後自動添加一個標準c段

[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
[apple@sgg-gatewy] > :put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)
221.232.119.0/24
[apple@sgg-gatewy] > :global testip
[apple@sgg-gatewy] > :put $testip  

[apple@sgg-gatewy] > :set testip 202.103.24.0/24  
[apple@sgg-gatewy] > :put $testip              
202.103.24.0/24
[apple@sgg-gatewy] > :set testip [:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]            
221.232.119.0/24
[apple@sgg-gatewy] > :put $testip                                                                            

[apple@sgg-gatewy] > :set testip {[:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]}
[apple@sgg-gatewy] > :put $testip                                                                              
[:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]
[apple@sgg-gatewy] > :set testip ([:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)])
221.232.119.0/24
[apple@sgg-gatewy] > :put $testip                                                                              

[apple@sgg-gatewy] > :environment print                                                                        
Global Variables
Local Variables
testip=

現在的需要解決的是,使用什麼方法,或者用別的命令來實現把連接好了的新數字賦給testip這個變量
直接腳本里面寫dst-address=[:put (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)]
也是無效的的,測試過了的
用put,直接輸出了,不能完成賦值
不用put,不能連接兩個字段
大家一起思考思考,先表示感謝


:foreach i in=[/ip firewall address-list find list=tel***-stable ] do=[/ip route rule add dst-address=(([/ip firewall address-list get $i address ] & 255.255.255.0) . \/24) table=tel action=lookup disabled=no comment="tel*** add by lcnja`s auto script" src-address=0.0.0.0/0]

呵呵,自己搞定了

---------------------------------------------------------------------------------------------------------------------------------------------

RouterOS2.96腳本詳解
四種變量


global - 定義全局變量, 可以要所有的腳本中調用共享
local - 定義本地變量,只能在其所要的腳本下調用,不能被其它腳本共享
loop index variables - 定義在for或foreach裏的索引號變量
monitor variables - 監視變量


ROS算術操作


- 負號;相減。
! 邏輯非。
/ 相除。
. 連接。兩個符串的連接,添加元素到列表
^ 異或(XOR)
~ 取反
* 相剩
& 與(AND)
&& 邏輯與
+ 相加
< 小於
<< 向左位移
<= 小於等於
> 大於
>= 大於等於
>> 向右位移
| 或
|| 邏輯或
ROS說明書裏的例子:


計算順序


[admin@MikroTik]> :put (10+1-6*2=11-12=2+(-3)=-1)
false
[admin@MikroTik]> :put (10+1-6*2=11-12=(2+(-3)=-1))
true


邏輯非


[admin@MikroTik]> :put (!true)
false
[admin@MikroTik]> :put (!(2>3))
true


數位取反


[admin@MikroTik]> :put (~255.255.0.0)
0.0.255.255


加法


[admin@MikroTik]> :put (3ms + 5s)
00:00:05.003
[admin@MikroTik]> :put (10.0.0.15 + 0.0.10.0)
cannot add ip address to ip address
[admin@MikroTik]> :put (10.0.0.15 + 10)
10.0.0.25


減法


[admin@MikroTik]> :put (15 - 10)
5
[admin@MikroTik]> :put (10.0.0.15 - 10.0.0.3)
12
[admin@MikroTik]> :put (10.0.0.15 - 12)
10.0.0.3
[admin@MikroTik]> :put (15h - 2s)
14:59:58


乘法


[admin@MikroTik]> :put (12s * 4)
00:00:48
[admin@MikroTik]> :put (-5 * -2)
10


除法


[admin@MikroTik]> :put (10s / 3)
00:00:03.333
[admin@MikroTik]> :put (5 / 2)
2
[admin@MikroTik]> :put (0:0.10 / 3)
00:00:02


比較


[admin@MikroTik]> :put (10.0.2.3<=2.0.3.10)
false
[admin@MikroTik]> :put (100000s>27h)
true
[admin@MikroTik]> :put (60s,1d!=1m,3600s)
true
[admin@MikroTik]> :put (bridge=routing)
false
[admin@MikroTik]> :put (yes=false)
false
[admin@MikroTik]> :put (true=aye)
false


邏輯與 AND, 邏輯或 or


[admin@MikroTik]> :put ((yes && yes) || (yes && no))
true
[admin@MikroTik]> :put ((no || no) && (no || yes))
false


數位與AND, 或OR, 異或XOR


[admin@MikroTik]> :put (10.16.0.134 & ~255.255.255.0)
0.0.0.134


位移操作


[admin@MikroTik]> :put (~((0.0.0.1 << 7) - 1))
255.255.255.128


連接操作


[admin@MikroTik]> :put (1 . 3)
13
[admin@MikroTik]> :put (1,2 . 3)
1,2,3
[admin@MikroTik]> :put (1 . 3,4)
13,4
[admin@MikroTik]> :put (1,2 . 3,4)
1,2,3,4
[admin@MikroTik]> :put ((1 . 3) + 1)
14
[admin@MikroTik]> :set a "It's "
[admin@MikroTik]> :put ($a . OK)
It's OK


ROS腳本保留字


beep execute global list pick time toip typeof
delay find if local put toarray tonum while
do for led log resolve tobool tostr
environment foreach len nothing set toid totime


聲音和警報


:beep length=2s frequency=10000
產生2秒10kHz的音頻
length缺省值爲100ms
frequency缺省值爲1000Hz



execute調用其他命令或者腳本


:set a "/int dis lan\n/int dis wan"
:execute $a
執行多條命令,例子裏執行了兩條命令。\n是換行



:global
定義全局變量的使用


:list interface
顯示相關命令。顯示當前目錄及子目錄下有關interface的命令



:pick 數組和字串的截取


取字符串或數組的某一斷。字符串(數組)的第一個爲0。
[admin@MikroTik]>:put [:pick "I love you" 2 6]
love


:time 計算命令執行所用時長


執行命令所需的時間
[admin@MikroTik]> :put [:time [:resole www.sina.com.cn]]
00:00:00.006
執行解析www.sina.com.cn這個域名所需的時間



數據類型轉換


toip toarray tonum tobool tostr toid totime
轉換值類型



延遲 多少秒


:delay 3
延時3秒,缺省爲1秒


:find 查找功能


查找字符串或數組中第一個出現查找內容的位置
[admin@MikroTik]>:put [:find abcdcba cd]
2
[admin@MikroTik]>:put [:find "1,2,3,4,3,2,1" 2]
1



:put 屏幕輸出


輸出到屏幕上,上面就很多例子了。



:if 條件判斷
條件選擇
[admin@MikroTik]>:if(1<2) do={:put true}
true
如果條件爲真,執行do={}裏面的命令
[admin@MikroTik]>:if(1>2) do={:put true} else={:put flase}
flase
如果條件爲真,執行do={}裏面的命令,否則執行else={}裏有命令
[/hide]
:local 局部變量的使用


定義本地變量
:local myip
局部變量只能在某個腳本或者命令行使用,其他的腳本或者命令行不能調用


:while 循環


條件爲真時循環執行do={}裏的腳本命令
[admin@MikroTik]>:set i 0;:while($i<5) do={:put $i;:set i ($i+1)}
0
1
2
3
4
5


:for 循環


循環執行do={}裏的腳本命令
:for i from=1 to=100 step=30 do={:put $i}
1
31
61
91


:foreach 循環


在集合(數組)裏循環執行do={}裏的腳本命令
:foreach i in=[/interface find type=ether] do={:put [/interface get $i name]}
ether1
ether2
ether3
ether4
在[/interface find type=ether]的集合(類型爲ether的interface ID)循環輸出

interface的名稱。我這時有四張網卡



:log 日誌類型 日誌內容
寫日誌操作


寫文本到日誌(script log)
類型在/system logging裏可以找到
:log info "系統信息"
到LOG裏查查運行結果吧



:resolve
域名解析


解析域名的IP地址
[admin@MikroTik] > :put [:resolve www.sina.com.cn]
61.172.201.240



:environment print
顯示所有變量及其值

:len
字符串或數組的長度


[admin@MikroTik] > :put [:len hello]
5
[admin@MikroTik] > :put [:len "1,2,23,65,54,6"]
6



:nothing
空值。nothing不等0,不等於空字符""
:find abc a的結果是0
:find abc d的結果是nothing

:set
賦值


[admin@MikroTik] > :set a test
將abc字符賦給變量a
[admin@MikroTik] > :put $a
test
[admin@MikroTik] > :put a
a



變量的引用
引用變量的值要在變量前面加$
如下
[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
我們先查看一下環境,顯示沒有如何的全局或者局部變量
[apple@sgg-gatewy] > :global testip
我們定義一個全局變量
[apple@sgg-gatewy] > /ip firewall address-list print
Flags: X - disabled, D - dynamic
#   LIST          ADDRESS                        
0 D tel***        221.232.119.228                
1   tel***-stable 221.232.119.228    
查看一下address-list有沒數據,因爲我們下面使用序號查詢了,所有首先需要使用pr命令顯示一下,要不回失敗,也可以不按序號查就可以不需要這個          
[apple@sgg-gatewy] > :set testip (([/ip firewall address-list get 0 address ] & 255.255.255.0) . \/24)  
使用set對testip變量賦值,上面是個複製的腳本來給testip賦值的,當然你用:set testip 3
這樣的簡單的來給testip賦值也是可以的
[apple@sgg-gatewy] > :environment print
Global Variables
Local Variables
testip=221.232.119.0/24
我們查看環境,現在有了一個變量,並且已經有了數值了

下面使用put來輸出變量的值到屏幕
$testip
用這個方式來表示testip變量的數值
[apple@sgg-gatewy] > :put $testip
221.232.119.0/24
[apple@sgg-gatewy] > 

                                              
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章