使用Winbox给MikroTik添加定时脚本(实用)

软路网络 盛行 3个月前 (07-14) 315次浏览 未收录 0个评论 扫描二维码
文章目录
[隐藏]

1.简介

在mikrotik中添加自定义脚本可以很方便的处理一些重复性的工作,像这些重复的工作可以利用任务计划功能得到解决,以下是我收集到的一些脚本,很实用分享出来。

2.相关

MikroTik:v5.25
Winbox:v2.2.18

3.效果

使用Winbox给MikroTik添加定时脚本(实用)

4.步骤

打开winbox连接到mikrotik,点击左侧的SystemScheduler-点击“+”号新建任务

4.1.过期踢人

使用Winbox给MikroTik添加定时脚本(实用)
Name:任务名称(随便取支持中文)
Start Date:开始日期
Start Time:开始时间
Interval:间隔(也就是多久执行一次任务,格式:天 时:分:秒)

请校对MiktoTik系统时间,否则可能任务不会生效

:local nyr [/system clock get date]
:local month ([:find jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec [:pick $nyr 0 3]]+1)
:if ($month<10) do={:set month (0 . $month)}
:set nyr ([:pick $nyr 7 11] . $month . [:pick $nyr 4 6])
:foreach user in [/ppp secret find comment<=$nyr] do={
/ppp active remove [find name=[/ppp secret get $user name]];
/ppp sec set $user disabled=yes
}

4.2.DDNS域名解析

#本DDNS脚本默认更新线路为PPPOE-OUT1,如果PPPOE-OUT1掉线状态则更新PPPOE-OUT2
:do {
:local ether
#DDNS帐号
:local user  "帐号"
#DDNS密码
:local pass  "密码"
#DDNS域名
:local host  "域名"
#以下不需要修改
:local status [/interface pppoe-client get [/interface pppoe-client find name="pppoe-out1"] running];
:if ($status=true) do={
:set ether "pppoe-out1"};
:if ($status=false) do={
:set ether "pppoe-out2"};
:local oldip [:resolve $host]
:delay 5s
:local newip [ /ip address get [/ip address find interface=$ether ] address ]
:set newip [:pick $newip 0 [:find $newip "/"]]
:if ($oldip != $newip) do={
/tool fetch url=("http://members.3322.org/dyndns/update\?system=dyndns&hostname=\
$host&myip=$newip") mode=http user=$user password=$pass dst-path=$host
:log error ("\BE\C9\B5\C4IP\B5\D8\D6\B7\A3\BA: ".$oldip)
:log error ("\D0\C2\B5\C4IP\B5\D8\D6\B7\A3\BA: ".$newip)
:delay 3s
:local Result [/file get $host contents]
:if ([:find $Result "good"] != "nil") do={
:log error ($host." \B8\FC\D0\C2\B3\C9\B9\A6")
} else={
:log error ($host." \B8\FC\D0\C2\CA\A7\B0\DC")}
/file remove $host}}
本脚本选项参数参考上面的《过期踢人》,将公云的“帐号”“密码”“域名”替换成自己的即可
:local ednsuser "帐号"
:local ednspass "密码"
:local ednshost "域名"
:local ednsinterface "pppoe-out1"
:local members "http://members.3322.org/dyndns/update?system=dyndns"
: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 info ($ednshost . " " .$result)
/file remove $ednshost ;
}
}
本脚本选项参数参考上面的《过期踢人》,将公云的“帐号”“密码”“域名”替换成自己的即可

4.3.自动连接

{
 :local status
 :local i "10"
 :local x "0"
 :local y "0"
 :local z "0"
 :set x [:len [/interface pppoe-client find running=yes]]
 :if ($x<$i) do={ :for ii from=1 to=$i do={ :set status [/interface get [find name=("pppoe-out".$ii)] running] :if ($status=true) do={ /ip fir man set [find new-connection-mark=$ii] per-connection-classifier=("both-addresses:".$x."/".$y) disable=no;:set y ($y+1)} else={ /ip fir man set [find new-connection-mark=$ii] disable=yes}}} :if ($x=$i) do={ :set z [:len [/ip fir man find action="mark-connection" disabled=yes chain=prerouting]] :if ($z>0) do={
 :for ii from=1 to=$i do={
 /ip fi man set [find new-connection-mark=$ii] per-connection-classifier=("both-addresses:".$x."/".$y) disable=no;:set y ($y+1)}}}}

4.4.PPPOE到期提醒

# 脚本名称: ExpAuto
# 脚本版本: 1.03
# 版本日期: 2011年1月3日
# 适用系统: (实例为 PPPoE到期 Web Proxy 错误页面通知,提前3天通知用户,到期停止用户,3.30测试通过,其他版本未测试)
# 脚本功能: 对PPP及Hotspot用户期限操作;此脚本的实例对用户状态(禁用/启用)进行设置及剔除到期在线用户,添加其他功能可参考下面例子
# 其他功能: 通过注释添加system scheduler星期制的禁用启用,注释格式:[7位布尔值]第一位是星期日,第二位是星期一,以此类推,E为启用,D为禁用
# 例:[EDDDDDE]代表只有周六日启用该scheduler,[DEEEEED]这个刚好相反,只有星期一至五启用该scheduler;
# 注意:被设置状态(启用/禁用)的scheduler运行时间不能调整到刚好00:00:00,因为00:00:00刚好在执行此脚本,未能00:00:00就设置好状态

# 添加用户注释日期格式:
# 例1:"张三[20100608]到期"(双引号里面为注释内容,[]里面为日期八位数字),
# 例2:"李四[+365]到期",[+365]指当前日期加365天,在下次调用脚本时注释将自动变为"李四[八位当前日期+365]到期",
# 假设当前日期为2010年4月4日立即调用脚本注释内容将变为"李四[20100404+365]到期",也就是2010年4月4日的365天后到期
# 例3:支持累加运算,假设[20090501+365]将近到期,用户续费可以在后面添加+X天,[20090501+365+180]这里加180天

#获取公元至今日( $Today 变量为公元至今已过的天数)
:local Date [/system clock get date];:local Year ([:pick $Date 7 11]-1)
:local Month ([:find jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec [:pick $Date 0 3]])
:local Day [:pick $Date 4 6];:if ([:pick $Date 4 5] = 0) do={:set Day [:pick $Date 5 6]};:set Day ($Day-1)
:local leapyear 0,31,59,90,120,151,181,212,243,273,304,334,0,31,60,91,121,152,182,213,244,274,305,335
:global Today ($Year*365+$Year/4);:if (($Year-$Year/4*4) != 3) do={:set Today ($Today+[:pick $leapyear $Month]+$Day)} else={:set Today ($Today+[:pick $leapyear ($Month+12)]+$Day)}
:local usname;:local uspfl;:local Ccomment;:local lc;:local rc;:local Exp;:local js;:local pcl;:local CYear;:local CMonth;:local CDay;:local CToday

#利用公元天数计算今天是星期几,0代表星期日
:global Week ($Today-$Today/7*7)
#:log info ("今天是星期" . $Week)

#scheduler处理开始
/system scheduler
:foreach i in=[find comment!="" name!="ExpAuto"] do={
:set Ccomment [get $i comment];:set lc [:find $Ccomment "["];:set rc [:find $Ccomment "]"]
:if ([:typeof $lc]="num" && [:typeof $rc]="num" && ($lc+1+$Week)<$rc) do={
:set Exp [:pick $Ccomment ($lc+1+$Week)]
:if ($Exp="e"||$Exp="E"||$Exp="1") do={:if ([get $i disabled ]) do={enable $i}}
:if ($Exp="d"||$Exp="D"||$Exp="0") do={:if (![get $i disabled ]) do={disable $i}}
}}
#scheduler处理结束


#PPP处理开始
/ppp secret
:foreach i in=[find comment!=""] do={
:set Ccomment [get $i comment];:set lc [:find $Ccomment "["];:set rc [:find $Ccomment "]"]
:if ([:typeof $lc]="num" && [:typeof $rc]="num" && $lc<$rc) do={ :set Exp [:pick $Ccomment ($lc+1) $rc];:set js 0;:set CToday 0 :if ([:pick $Exp 0 1]!="+" && [:len $Exp]>7 && [:typeof [:tonum [:pick $Exp 0 8]]]="num") do={
:set usname [get $i name];:set uspfl [get $i profile];:set CYear ([:pick $Exp 0 4]-1);:set CToday ($CYear*365+$CYear /4)
:set CMonth [:pick $Exp 4 6];:if ([:pick $Exp 4 5] = 0) do={ :set CMonth [:pick $Exp 5 6]};:set CMonth ($CMonth-1)
:set CDay [:pick $Exp 6 8];:if ([:pick $Exp 6 7] = 0) do={ :set CDay [:pick $Exp 7 8]};:set CDay ($CDay-1)
:if (($CYear-$CYear/4*4) != 3) do={:set CToday ($CToday+[:pick $leapyear $CMonth]+$CDay)} else={:set CToday ($CToday+[:pick $leapyear ($CMonth+12)]+$CDay)}}
:set pcl [:find $Exp "+"];:if ([:typeof $pcl]="num" && ($pcl>7 || $pcl=0) && ([:len $Exp]-1)>$pcl) do={
:if ($pcl = 0) do={:set CToday $Today;:set CYear ($Year+1);:set CMonth ($Month+1);:set CDay ($Day+1)
:if ([:len $CMonth]<2) do={:set CMonth ("0" . $CMonth)};:if ([:len $CDay]<2) do={:set CDay ("0" . $CDay)}
set $i comment=([:pick $Ccomment 0 $lc] . "[" .($CYear . $CMonth . $CDay) . [:pick $Exp $pcl [:len $Exp]] . "]" . [:pick $Ccomment ($rc+1) [:len $Ccomment]])}
:while ([:typeof [:find $Exp "+"]]="num") do={
:set Exp [:pick $Exp ($pcl+1) [:len $Exp]];:set pcl [:find $Exp "+"];:if ([:typeof $pcl]!="num") do={:set pcl [:len $Exp]}
:set js ($js+[:tonum [:pick $Exp 0 $pcl]])};:set CToday ($CToday+$js)}
#例子1:禁用并剔出到期PPP用户
:if ($CToday < $Today) do={:if (![get $i disabled ]) do={disable $i;/ppp active remove [find name=$usname]}} else={:if ([get $i disabled ]) do={enable $i}}
#例子2:提前3天踢出到期用户并给予设置到期规则
:if (($CToday-3) < $Today) do={:if (![get $i disabled ] && [:pick $uspfl 0 8]!="ExpAuto_") do={
:if ([:len [..profile find name="ExpAuto_" . $uspfl]]=0) do={..profile add name=("ExpAuto_" . $uspfl) copy-from=$uspfl address-list=ExpAuto_expired}
set $i profile=("ExpAuto_" . $uspfl);/ppp active remove [find name=$usname]}} else={:if ([:pick $uspfl 0 8]="ExpAuto_") do={
:if ([:len [..profile find name=[:pick $uspfl 8 [:len $uspfl]]]]=0) do={..profile add name=[:pick $uspfl 8 [:len $uspfl]] copy-from=$uspfl address-list=""}
set $i profile=[:pick $uspfl 8 [:len $uspfl]];:foreach acus in=[/ppp active find name=$usname] do={:local tempipaddr [/ppp active get $acus address]
/ip firewall address-list remove [find list=ExpAuto_expired address=$tempipaddr];/ip firewall address-list remove [find list=ExpAuto_allow address=$tempipaddr]}}}
#根据需要可添加或替换对用户的操作例子,假设提前5天,则在条件:if (($CToday-5) < $Today) do={执行通知操作…}
#:if (($CToday-5) < $Today) do={执行通知操作…}

}}
#PPP处理结束



#Hotspot处理开始
/ip hotspot user
:foreach i in=[find comment!=""] do={
:set Ccomment [get $i comment];:set lc [:find $Ccomment "["];:set rc [:find $Ccomment "]"]
:if ([:typeof $lc]="num" && [:typeof $rc]="num" && $lc<$rc) do={ :set Exp [:pick $Ccomment ($lc+1) $rc];:set js 0;:set CToday 0 :if ([:pick $Exp 0 1]!="+" && [:len $Exp]>7 && [:typeof [:tonum [:pick $Exp 0 8]]]="num") do={
:set usname [get $i name];:set uspfl [get $i profile];:set CYear ([:pick $Exp 0 4]-1);:set CToday ($CYear*365+$CYear /4)
:set CMonth [:pick $Exp 4 6];:if ([:pick $Exp 4 5] = 0) do={ :set CMonth [:pick $Exp 5 6]};:set CMonth ($CMonth-1)
:set CDay [:pick $Exp 6 8];:if ([:pick $Exp 6 7] = 0) do={ :set CDay [:pick $Exp 7 8]};:set CDay ($CDay-1)
:if (($CYear-$CYear/4*4) != 3) do={:set CToday ($CToday+[:pick $leapyear $CMonth]+$CDay)} else={:set CToday ($CToday+[:pick $leapyear ($CMonth+12)]+$CDay)}}
:set pcl [:find $Exp "+"];:if ([:typeof $pcl]="num" && ($pcl>7 || $pcl=0) && ([:len $Exp]-1)>$pcl) do={
:if ($pcl = 0) do={:set CToday $Today;:set CYear ($Year+1);:set CMonth ($Month+1);:set CDay ($Day+1)
:if ([:len $CMonth]<2) do={:set CMonth ("0" . $CMonth)};:if ([:len $CDay]<2) do={:set CDay ("0" . $CDay)}
set $i comment=([:pick $Ccomment 0 $lc] . "[" .($CYear . $CMonth . $CDay) . [:pick $Exp $pcl [:len $Exp]] . "]" . [:pick $Ccomment ($rc+1) [:len $Ccomment]])}
:while ([:typeof [:find $Exp "+"]]="num") do={
:set Exp [:pick $Exp ($pcl+1) [:len $Exp]];:set pcl [:find $Exp "+"];:if ([:typeof $pcl]!="num") do={:set pcl [:len $Exp]}
:set js ($js+[:tonum [:pick $Exp 0 $pcl]])};:set CToday ($CToday+$js)}
#例子1:设置Hotspot用户状态(禁用并剔除和启用)
:if ($CToday < $Today) do={:if (![get $i disabled ]) do={disable $i;/ip hotspot active remove [find name=$usname]}} else={:if ([get $i disabled ]) do={enable $i}}
#:if (($CToday-5) < $Today) do={执行的操作…}

}}
#Hotspot处理结束
此脚本可以放到任务计划里面执行或是放到脚本里面,然后利用任务计划直接调用脚本名称即可,脚本添加点击System-Scripts-点击“+”号即可添加

IT博客站版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用Winbox给MikroTik添加定时脚本(实用)
喜欢 (0)
[使用支付宝扫码]
分享 (0)
盛行
关于作者:
企业应用技术爱好者,喜欢分享知识。如果文章中有错误,欢迎指正,谢谢。
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址