Example of Mikrotik Script to execute customized function IF condition matches !
Requirements:
- Mikrotik should disconnect wan client pppoe-out1 , only if its uptime is above 8 hours for that single session. There should be several checks like the script should check for following (to avoid errors)
- Check for valid interface name , if interface not found, print error
- Check for Running Status, if the pppoe-out interface is not connected to ppp server, print error
- If the pppoe-out client uptime is above then the defined maximum uptime limit, it should disable interface, wait for 5 seconds, and then re-connect
- Check if interface is in running status, print its IP address an strip its subnet
Solution:
the Script !
# Mikrotik Script to monitor UPTIME of the PPPOE-OUT(x) WAN interface, and act accordingly # Useful when you want to disconnect your wan interface for any reason if it uptime crosses max up time limit defined. # Maybe to hide router form Remote PPP Servers uptime Monitoring System # Syed Jahanzaib / aacable @ hotmail . com / https://aacable.wordpress.com # Tested with Mikrotik 6.35.2 / on RB3011UiAS (arm) # Created: 8-JUN-2016 # Define which interface uptime you want to monitor, in this example, i used pppoe-out1, you may change it as required :local PPPINT pppoe-out1; # Define the UPTIME Limit that will be matched with uptime, Example is 8 hours :local MAXUPTIMELIMIT 08:00:00; # Define how long it should wait before re-connecting / re-enable wan interface, example 5 seconds :local DELAY 5s; ############################### # SCRIPT FUNCTIONS STARTS HERE ... ############################### # Check if interface is available or not, IF NOT THEN EXIT :if ([:len [/interface find name=$PPPINT]] = 0 ) do={ :log error "WARNING: No interface named $PPPTINT, please check configuration." } # Check for Interface Running Status, if its not connected then give error :global PPPINTSTATUS; /interface pppoe-client monitor $PPPINT once do={ :set PPPINTSTATUS $status} :if ($PPPINTSTATUS != "connected") do={ :log error "$PPPINT NOT CONNECTED TO THE REMOTE SERVER YET, NOT READY"; } # Define variable to hold current uptime value of the interface :local wanuptime; /interface pppoe-client monitor $PPPINT once do={ :set $wanuptime $uptime; # Print Uptime , just for testing #:log warning "$PPPINT UP Time is > $uptime"; } # Match interface current uptime with maximum uptime limit defined in MAXUPTIMELIMIT variable # Greater then forumla / zaib :if ($wanuptime>$MAXUPTIMELIMIT) do={ :log error "ALERT: $PPPINT UP Time have crossed $MAXUPTIMELIMIT Hours Limit ... Disconnecting it and will Re-Connect after 5 Seconds"; # Disable $PPPINT interface /interface disable $PPPINT delay $DELAY; /interface enable $PPPINT :log warning "$PPPINT have been enabled , check if its connected properly." :delay $DELAY :local PPPINTIP [ /ip address get [/ip address find interface=$PPPINT] address ] :for i from=( [:len $PPPINTIP] - 1) to=0 step=-1 do={ :if ( [:pick $PPPINTIP $i] = "/") do={ :set PPPINTIP [:pick $PPPINTIP 0 $i] :log warning "$PPPINT ip address is > $PPPINTIP / Script ENDS here ..." } } # Show under limit message. if all ok :if ([/interface get $PPPINT value-name=running]) do={ :if ($wanuptime<$MAXUPTIMELIMIT) do={ :log warning "$PPPINT UP-Time $wanuptime is under $MAXUPTIMELIMIT Hours Limit / Script ENDS here ..."; :local PPPINTIP [ /ip address get [/ip address find interface=$PPPINT] address ] :for i from=( [:len $PPPINTIP] - 1) to=0 step=-1 do={ :if ( [:pick $PPPINTIP $i] = "/") do={ :set PPPINTIP [:pick $PPPINTIP 0 $i] :log warning "$PPPINT ip address is > $PPPINTIP" } } } } }
Regard’s
Syed Jahanzaib
Filed under: Mikrotik Related
