Syed Jahanzaib Personal Blog to Share Knowledge !

April 4, 2016

Multiple IF statement matching with Mikrotik Script

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

mqtching

:if (($dsl1 = "DOWN") && ($dsl2 = "DOWN")) do={

Note: This is a draft version of a solution i made for an OP, donot copy paste, its just showing a different way of achieving the goal by customized scripting in Mikrotik. Surely there are other proper , more sophisticated methods out there, its just a way among them for a lamer like me 😉


 

SCENARIO:

We have two WAN DSL links in our RB with PCC configured. Now 3rd wan link is being attached and it should be used as failover only. Means if both DSL fails, then the master script should disable routing to both dsl, and enable 3rd wan link and send sms as well. In this example we are using VARIABLES from existing scripts and take decision based on those variables.

I used KANNEL as SMS gateway to send sms alerts. [installed on a local linux system]

Script:

WAN1MON = it checks for DSL 1 status and update variable ‘wan1staus’

WAN2MON = it checks for DSL 2 status and update variable ‘wan1staus’

Now we will make 3rd script name MASTER [ described in this post ] which will check for variable values created by above scripts. if it found value DOWN for both wan links, it will simply disable the PCC rules, and will enable simple NAT rule.

I cannot explain the script in details dueto time shortage, but this script was created for some customized purpose and it fulfilled the requirements well , at least for particular OP.



# Mikrotik MASTER WAN Link monitoring SCRIPT with optional SMS Alert,
# We are using local KANNEL as SMS gateway
# by Syed Jahanzaib
# https://aacable.wordpress.com
# Email : aacable at hotmail dot com
# Script Last Modified : 4th-APR-2016 / 1300 hours
# Setting VARIABLES
:local date;
:local time;
:set date [/system clock get date];
:set time [/system clock get time];
:local cell1 "03333021909"

# Company Name, donot use spaces in it
:local COMPANY "JZ"

#If you dont have kannel sms gateway ignore this.
:local KURL "http://192.168.100.1:13013/cgi-bin/sendsms"
:local KID "kannel"
:local KPASS "kannelpass"


:global masterstatus
:local dsl1 [/system script environment get [/system script environment find name="wan1netstatus"] value];
:local dsl2 [/system script environment get [/system script environment find name="WAN2netstatus"] value];

# SMS Msg format for Kannel SMS gateway (donot use spaces in it)
:local MSGDOWNSMS "$COMPANY+WAN+ALERT:%0AWAN1-and-2+is+now+DOWN."

#Match condition
:if (($dsl1 = "DOWN") && ($dsl2 = "DOWN")) do={
:if (($masterstatus="UP")) do={
set masterstatus "DOWN";

# If both vdsl found dead, then enable backup link
:log error "BOTH VDSL SEEMS TO BE DOWN, ACTIVATING BACKUP LB - BACKUP LINK ... Powered by SYED JAHANZAIB"

# Send SMS via KANNEL FOR UP ALERT
/tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell2&text=$MSGDOWNSMS"

#INSERT YOUR RULES HERE FOR DOWN ACTION
#/ip firewall mangle disable [find comment="PCC-1"]
#/ip firewall mangle disable [find comment="PCC-2"]
#/ip firewall mangle disable [find comment="PCC-3"]
#/ip firewall mangle disable [find comment="PCC-4"]
#/ip firewall mangle disable [find comment="PCC-5"]
#/ip firewall mangle disable [find comment="PCC-6"]
#/ip firewall nat enable [find comment="BACKUP_NAT_ENABLE_IT_WHEN_VDSL_DOWN"]

# If one or both vdsl seems to be alive, then do nothing, just skip
} else={:set masterstatus "DOWN";}
} else={
:if (($masterstatus="DOWN")) do={
:set masterstatus "UP";
:log warning "At least one or both vdsl seems to be UP... Skipping ... Powered by Syed JahanzaiB"</pre>
#INSERT YOUR RULES HERE FOR UP ACTION
#/ip firewall mangle enable [find comment="PCC-1"]
#/ip firewall mangle enable [find comment="PCC-2"]
#/ip firewall mangle enable [find comment="PCC-3"]
#/ip firewall mangle enable [find comment="PCC-4"]
#/ip firewall mangle enable [find comment="PCC-5"]
#/ip firewall mangle enable [find comment="PCC-6"]
#/ip firewall nat disable [find comment="BACKUP_NAT_ENABLE_IT_WHEN_VDSL_DOWN"]

:local MSGUPSMS "$COMPANY+WAN+INFO:%0AWAN-1-2+have+been+Restored.%0AFailover+Script+Powered+By+Syed+Jahanzaib."

# Send SMS via KANNEL FOR UP ALERT
/tool fetch url="$KURL\?username=$KID&password=$KPASS&to=$cell2&text=$MSGUPSMS"
}
}
} 

Regard’s
Syed Jahanzaib

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: