This is not easy 3 line script. Some theory must be add.
Note: NetWatch when run UP-script or DOWN-script create a new job every times and owner of job is *sys user and previous/opposite job is not kill. Few changes of ISP and Netwatch run parallel jobs.
Multiple instances of up&down-script scripts duplication are the same job property, we can not select what job is up or down script.
They have *many limitation as sys user like global variable are real a local ones. We have not permission to remove a real one globals or create real one. Cannot create new script. We can create scheduler who can create scripts but all operations are with *sys user and are still limited.
Other limitation are: Netwatch not check few times until change state, Use only main route table, cannot use a specific src-address.
.
Very wrong situation. On some limitation we can use solution/workaround like:
- Use a separated “System Script” to identify scripts in script\jobs tab. We must use the “dont-require-permissions=yes” becuase NetWatch use *sys user but our system script use our logon user.
- Kill opposite one if still exist by e.g. /system script job remove [find script~“SomeOppositeScript”] but it must run as separate system script
Note: “Improved NetWatch II script” create a big infinite “:do loop” with additional pings who have latency and timeout, to many loops inside NetWatch itself. Additional NetWatch do his ping of state and we do pings from inside script’s = dual ping from our router. Good example of use it is at “Enhanced NetWatch by manojlovicl”
Note: Main JOB is to do Re-Connect USB LTE connection
- Turn off Power on USB port, not work at all RouterBoard with USB, list of devices are in https://wiki.mikrotik.com/wiki/Manual:USB_Features#RouterBoard_USB_port_table like: RB750UP, RB751G-2HnD and RB751U-2HnD.
e.g. /system routerboard usb power-reset duration=3; - Just re-enable lte interface
e.g. /interface lte disable [find]; delay 3s; /interface lte enable [find]; - at the end we must do a Reboot of MikroTik device.
e.g. /system reboot
other) of course are other like… modem can use PPP dial to re-connection or use a AT command is modem accept it bo change LTE Band, etc. etc.
My idea and solution.
Why anyone not thinking about NetWatch properties like Status && Since - both give us good feedback without additional own ping-loops.
We can create a simple scheduler from down-script to do a Main Job with limitation of *sys user to do our job in future like old “logic bomb” in virus terminology.
Simple solutions are the best, they say “Trust me, I’m an engineer” too
Check this out.
SiB way example:
1. Create NetWatch → only it will do ping testing of target host and run up/down-script.
/tool netwatch add host=1.1.1.1 interval=10s timeout=500ms disabled=no comment=RePowerUSB
*) 2. Add UP-script → Remove future action because ISP is up.
Do Nothing more.
/system scheduler remove [find where name~"SiBexRePowerUSBv"];
*) 3a. Add DOWN-script → ReCreate future jobs before DeadLine came true in 5m or 7m and last hope do reboot in 15m.
# Add action to future via scheduler
/system scheduler remove [find where name~"SiBexRePowerUSBv"];
local now [/system clock get time];
/system scheduler add name=SiBexRePowerUSBv1 on-event={/system routerboard usb power-reset duration=3;} start-time=($now+5m );
/system scheduler add name=SiBexRePowerUSBv2 on-event={/interface lte disable [find]; delay 3s; /interface lte enable [find];} start-time=($now+7m );
/system scheduler add name=SiBexRePowerUSBv3 on-event={/system reboot;} start-time=($now+15m );
You see the idea now.
This is not work good when we have problems with ISP happen at 23:59:59 and the scheduler must be created next day, means when we must Crossing Midnight.
3b. Final Netwatch DOWN-script with function who resolve the Crossing Midnight problem is:
log error "start Netwatch DOWN-script...";
# ################ MAIN PROGRAM BELOW, NOT HERE ######################
# ################ DEFINE FUNCTION SOLVING Crossing Midnight ##############
/local SiBFixDateTime do={
#/local SiBTimeToChange "23:45:00";
/local SiBTimeToChange $2;
#/local SiBTimeDiffToAdd "60m2s"; # diff time < 24h
/local SiBTimeDiffToAdd $3; # diff time < 24h
#/local SiBDate "dec/31/2020"
/local SiBDate $1
/local SiBDateCorrected;
#log warning ("Source: ".$SiBDate." ".$SiBTimeToChange." + ".$SiBTimeDiffToAdd);
#put ("Source: ".$SiBDate." ".$SiBTimeToChange." + ".$SiBTimeDiffToAdd);
# Add diff time <=24h
/local SiBTimeAndDiffTime ($SiBTimeToChange + $SiBTimeDiffToAdd );
/if ([/len $SiBTimeAndDiffTime]=8) do={/set $SiBDateCorrected ($SiBDate." ".$SiBTimeAndDiffTime); };
/if ([/len $SiBTimeAndDiffTime]>8) do={
/set $SiBTimeAndDiffTime ($SiBTimeAndDiffTime-1d);
## Add 24h to date, copy time if exist.
set $SiBDateCorrected ($SiBDate." ".$SiBTimeAndDiffTime);
# Days: 01-27
local SiBDayToChange [/pick $SiBDateCorrected 4 6;];
if ($SiBDayToChange<=27) do={
set $SiBDayToChange ($SiBDayToChange+1);
if ([/len $SiBDayToChange]=1) do={set $SiBDayToChange ("0".$SiBDayToChange)}
set $SiBDateCorrected ( [/pick $SiBDateCorrected 0 4].$SiBDayToChange.[/pick $SiBDateCorrected 6 20] );
} else={
if ($SiBDayToChange!=31) do={
# Days: 28..30; Main magic do internal scheduler.
/system scheduler remove [find name=SiBTempScheduler2qw34er5v78bn90m];
/system scheduler add name=SiBTempScheduler2qw34er5v78bn90m on-event={nothing } start-time=00:00:01 start-date=( [/pick $SiBDateCorrected 0 4].($SiBDayToChange+1).[/pick $SiBDateCorrected 6 11 ] );
set $SiBDateCorrected ([system scheduler get [find name=SiBTempScheduler2qw34er5v78bn90m] start-date].[/pick $SiBDateCorrected 11 20 ]);
/system scheduler remove [find name=SiBTempScheduler2qw34er5v78bn90m];
} else={
# Day 31 for Jan..Nov
/local SiBMonths ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
/local SiBMounthToChange [/pick $SiBDateCorrected 0 3];
for x from=0 to=10 do={if ([/pick $SiBMonths $x]=$SiBMounthToChange) do={ set $SiBDateCorrected ([pick $SiBMonths ($x+1)]."/01".[/pick $SiBDateCorrected 6 20 ]) }}
# Day 31 for Dec
if ($SiBMounthToChange="dec") do={ set $SiBDateCorrected ("jan/01/".([/pick $SiBDateCorrected 7 11]+1).[/pick $SiBDateCorrected 11 20] )}
};
};
};
#log warning ("New date time: ".$SiBDateCorrected);
#put ("New date time: ".$SiBDateCorrected);
:return $SiBDateCorrected
};
# ################ MAIN PROGRAM ###########################
# Select a proper Netwatch
local LocNetWatchHost 1.1.1.1;
local LocNetWatchHostComment "RePowerUSB";
# Still is status down?
local LocStatus [/tool netwatch get [find host=$LocNetWatchHost comment=$LocNetWatchHostComment ] status];
if ($LocStatus != "down") do={/quit;}
# Convert the Netwatch SINCE to separate date and time of current down-time.
local LocSince [/tool netwatch get [find host=$LocNetWatchHost comment=$LocNetWatchHostComment status=down ] since];
local LocSinceDate ([/pick $LocSince 0 11]);
local LocSinceTime ([/pick $LocSince 12 20]);
# Add action to future via scheduler
/system scheduler remove [find where name~"SiBexRePowerUSBv"];
# Try MAIN ACTION 1 - RePower USB port
local SiBCrossMid [$SiBFixDateTime $LocSinceDate $LocSinceTime 5m ];
local SiBStartDate [pick $SiBCrossMid 0 11];
local SiBStartTime [pick $SiBCrossMid 12 20];
/system scheduler add name=SiBexRePowerUSBv1 on-event={/system routerboard usb power-reset duration=3;} start-date=$SiBStartDate start-time=$SiBStartTime;
# Try MAIN ACTION 2 - ReEnable LTE interface
local SiBCrossMid [$SiBFixDateTime $LocSinceDate $LocSinceTime 8m ];
local SiBStartDate [pick $SiBCrossMid 0 11];
local SiBStartTime [pick $SiBCrossMid 12 20];
/system scheduler add name=SiBexRePowerUSBv2 on-event={/interface lte disable [find]; delay 3s; /interface lte enable [find];} start-date=$SiBStartDate start-time=$SiBStartTime;
# Try MAIN ACTION 3 - Reboot MikroTik RouterBoard
local SiBCrossMid [$SiBFixDateTime $LocSinceDate $LocSinceTime 11m ];
local SiBStartDate [pick $SiBCrossMid 0 11];
local SiBStartTime [pick $SiBCrossMid 12 20];
/system scheduler add name=SiBexRePowerUSBv3 on-event={/system reboot;} start-date=$SiBStartDate start-time=$SiBStartTime;
log error "stop Netwatch DOWN-script, Future action are scheduled";
.
This is working of few RouterOS with USB/Ethernet LTE and works very well. I hope anyone can use it because I cannot found at Forum better script and this post is the best to publish answer.
Source at GitHub too.