RouterOS SCript课程系列
第66课程 RouterOS Script语言
RouterOS script语言是RouterOS内置的,功能强大,语法灵活,脚本经常与命令结合,通过脚本,可以实现自动化操作,提高工作效率等,
RouterOS脚本语言非常的强大,是整个RouterOS的核心,是精通RouterOS的必由之路。
熟悉Script脚本的前提是熟悉RouterOS的CLI命令行操作。我们将利用几节课的时间来进行学习,那现在就开始吧。
11、 fetch命令操作 download and upload
这一节课,我们来学习一个非常重要的命令fetch,新版本增加了一些新功能,常常配合脚本来使用。
官方wiki: https://wiki.mikrotik.com/wiki/Manual:Tools/Fetch
支持https,http,ftp,sftp 可以post/get 为了安全,还可以设置certificate。具体的应用我们通过例子来说明。
参数:
address:操作对象地址
as-value:将结果保存到变量output设置的值
dst-path:目标文件所在的路径
host:domain name or virtual host
http-method:|delete|get|post|put; Default: get
keep-result:
mode:ftp|http|tftp {!} https; Default: http
src-path:源文件及所在路径
upload:
url:可以用来替代dst-path和src-path
1、通过ftp 下载
fetch address=192.168.88.2 src-path=conf.rsc
user=admin mode=ftp password=123 dst-path=123.rsc port=21
host="" keep-result=yes
直接下载:
/tool fetch url="http://www.mikrotik.com/img/netaddresses2.pdf" mode=http
/tool fetch url="https://i.mt.lv/pdf/archive/subnet_diagram_helper.pdf" mode=https
post信息到远程主机
/tool fetch http-method=post http-content-type="application/json" http-data="{"lat":"56.12","lon":"25.12"}" url="http://192.168.254.250/1.php"
从V6.43可以将结果保存到变量(数组变量)
{
:local result [/tool fetch url=http://10.0.0.1/disable_ether2.php as-value output=user];
:if ($result->"status" = "finished") do={
:if ($result->"data" = "0") do={
/interface ethernet set ether2 disabled=yes;
} else={
/interface ethernet set ether2 disabled=no;
}
}
}
disable_ether2.php
<?php
echo 0;
?>
使用sftp:(SSH File Transfer Protocol) V6.45beta50
/tool fetch url="sftp://10.155.126.200/home/x86/Desktop/50MB.zip" user=x86 password=root dst-path=disk1
/tool fetch url="sftp://192.168.254.246/script.txt" user=test password=test
3322
:local ednsuser "youname"
:local ednspass "yourpass"
:local ednshost "yoursite.f3322.net"
:local ednsinterface "pppoe-out1"
:local members "http://members.3322.org/dyndns/update?system=dyndns"
#:local members "http://members./nic/update?"
:local status
:local status [/interface get [/interface find name=$ednsinterface] running]
:if ($status!=false) do={
:local ednslastip [:resolve $ednshost]
:if ([ :typeof $ednslastip ] = nil ) do={ :local ednslastip "0" }
:local ednsiph [ /ip address get [/ip address find interface=$ednsinterface ] address ]
:local ednsip [:pick $ednsiph 0 [:find $ednsiph "/"]]
:local ednsstr "&hostname=$ednshost&myip=$ednsip"
:if ($ednslastip != $ednsip) do={
/tool fetch url=($members . $ednsstr) mode=http user=$ednsuser password=$ednspass dst-path=$ednshost
:delay 2
:local result [/file get $ednshost contents]
:log warning ($ednshost . " " .$result)
/file remove $ednshost ;
}
}
http://members.3322.org/dyndns/update?system=dyndns&hostname=$ednshost&myip=$ednsip
http://members.3322.org/dyndns/update?system=dyndns&hostname=psg1212.f3322.net&myip=121.12.13.21