Syed Jahanzaib – Personal Blog to Share Knowledge !

January 27, 2018

Mikrotik: Schedule script to run in specific day(s) of week only

Filed under: Mikrotik Related — Tags: , , , — Syed Jahanzaib / Pinochio~:) @ 1:48 PM


Scenario:

In mikrotik, we want to execute a script which should run on following days at given time. Monday to Friday , 8am Sounds easy enough! but unfortunately Mikrotik doesn’t provides flexible scheduler like windows which let you select days by just clicking. Therefore we have to create some smart function that calculate current day and pass the variable to action part in the script, which than compare variable with its specific days like mon-fri & if it matches then take action ELSE goto Sleep 🙂

Regard's
Syed Jahanzaib

Solution!

Script!

# Script to check current day name, and take action accordingly ONLY if matched with our defined days
# Example disable specific wan link only which is a quota base service but free in specific timings
# Made it for an OP who was using NAYATEL WAN links which are quota base.
# But good part is they provide free airtime (no quota count) from 8pm till 8am Daily & on saturday + sunday its total free as well
# Therefore OP wants to disable the link only from mondy to friday at 8am, but it should not disable if its saturday or sunday.
# unfortuantely In mikrotik we dont have option for selecting DAYS like in windows task scheudler, pitty ...
# This is why we will use 'work smarter, not harder' approach for this particular case.
# This script will send EMAIL using GMAIL account & SMS using local KANNEL base SMS server (installed locally with GSM modem with mobile SIM)
# Syed Jahanzaib / aacable [at] hotmail [for] com / 27-January-2018

# The script may contain Junk data as well, please trim it before using
# Let's START....
# Decalring Variable..

# ISP NAME
:local ISP "NAYATEL"
# Finding day of week function is by melboyscout (melboyscout [at] gmail.com)
:local date [/system clock get date]
:local result ""
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]
:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local dayc [:pick $date 5 6]
:local year [:pick $date 7 11]
# if the first char is a 0 (zero) only read last char, else script fails
:if ([:pick $date 4 5] = 0) do={ :set day ($dayc)}
:local sum 0
:local aaa 0
:local yyy 0
:local mmm 0
:local nmonth 1
:for mindex from=0 to=[:len $months] do={
:if ([:pick $months $mindex] = $month) do={:set nmonth ($mindex + 1) }
}
:set aaa ((14 - $nmonth) / 12)
:set yyy ($year - $aaa)
:set mmm ($nmonth + 12 * $aaa - 2)
:set sum (7000 + $day + $yyy + ($yyy / 4) - ($yyy / 100) + ($yyy / 400) + ((31 * $mmm) / 12))
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]
:put $result
# END Math Calculation

# Start IF matching to see if current day is what we required, if yes take action
# Example we want to run scripts on Monday , Tuesday, Wedensday, Thursday, Friday Only
:if (($result = "mon") || ($result = "tue") || ($result = "wed") || ($result = "thu") || ($result = "fri")) do={
:log warning "$result Detected , taking action that is disabling WAN links ... Powered by Syed.Jahanzaib"

## ACTION START to disable Wan links ##
# Start action
:local date;
:local time;
:local sub1 ([/system identity get name])
:local sub2 ([/system clock get time])
:local sub3 ([/system clock get date])
# GMAIL SMTP DYNAMIC Config Section, Make sure to change these values to match your's / Jz
# Gmail SMTP Address
:global GMAILSMTP
:set GMAILSMTP "64.233.167.108";
:local adminmail1 ADMIN1@hotmail.com
:local gmailid YOUR_GMAIL_ID@gmail.com
:local gmailuser YOUR_GMAIL_ID
:local gmailpass GMAIL_PASSWORD
:local gmailport 587
# Set EMAIL Subject Body
:local EMAILSUBJECT "$[/system clock get date] $[/system clock get time] -- ALL $ISP Links have been disabled at 8am."
:local EMAILBODY "ALL $ISP Links have been disabled at 8am.\n\n\n\n Auto Script for Mikrotik Designed by \n *** Syed_Jahanzaib ****"
# Set SMS body withou spaces, use plus sign for space
:local SMS "All+NAYATEL+Links+have+been+disabled+by+auto+script+at+8+am"
# Setting KANNEL INFO and SMS Number
:local KID "kannel"
:local KPASS "KANNEL_PASS"
:local KHOST "10.0.0.1:13013"
:local CELL1 "03333021909"
# Disable Wan Link(s)
/interface ethernet set ether1 disabled=yes
/interface set pppoe-nt1 disabled=yes
# Log action
:log error "$[/system clock get date] $[/system clock get time] -- ALL $ISP Links have been disabled at 8am by Auto Script."
# "Emailing the DOWN status. . . "
/tool e-mail send to="$adminmail1" password=$gmailpass subject="$EMAILSUBJECT" from=$gmailid server=$GMAILSMTP start-tls=yes body="$EMAILBODY"
# SENDING SMS for action DOWN
/tool fetch url="http://$KHOST/cgi-bin/sendsms\?username=$KID&password=$KPASS&to=$CELL1&text=$SMS"
## Disable ACTION ENDS HERE ##
##
# BUT If its Saturday & Sunday then do nothing
# else goto sleep
} else={
:log warning "Saturday - Sunday Detected , no need to disable wan links , Goto Sleep now ... Powered by Syed.Jahanzaib"
}
}
}

# END
# Syed Jahanzaib / UNICOM - 27-JAN-2018

6 Comments »

  1. Hello
    I need Some assistance from yuo, can we talk over email or any private chat. Its about hotspot installation mini ISP. Thanks.

    Like

    Comment by vinmao — February 3, 2018 @ 2:42 PM

  2. Thanks a lot!
    I used your script to turn on and off wifi on my MT

    Like

    Comment by maikel — April 4, 2018 @ 11:51 AM

  3. Hi Syed,
    Is it possible to run a script daily except certain predefined days? For example I wish to shut down the router daily at 10:00 pm, but keep it running on the holidays / festivals.

    Thanks

    Liked by 1 person

    Comment by Prashant — November 9, 2018 @ 12:45 PM

    • Yes you can. Schedule your script to run daily at 10pm,
      & use the IF Else statement , example if its Saturday & Sunday , then dont run, else RUN. I shows this example in this post. Just modify as per requirements.

      Like

      Comment by Syed Jahanzaib / Pinochio~:) — November 9, 2018 @ 2:07 PM

  4. yes it works, thank you

    Like

    Comment by Endy Aer — June 19, 2021 @ 8:41 AM


RSS feed for comments on this post. TrackBack URI

Leave a comment