Community discussions

MikroTik App
 
deadkat
just joined
Topic Author
Posts: 1
Joined: Sun Nov 15, 2020 11:14 pm

Critique my script please

Mon Nov 16, 2020 9:22 pm

Hello all! This is my first real forum post and I hope I can get some help. I've spent the last two-ish weeks working on these three scripts to detect if my LTE modem has hung and needs to be reset. can you please help me out with any grammar/syntax problems you see? I've never written more than 2-3 line scripts before and I want to make sure this works before trying to implement it.

All ofthese scripts will be running on an RBLHGR with an R11e-LTE6 modem. First is a script to detect if either of the next two scripts are currently running before starting the internet-detector:
#check if internet-detector running before starting new instance
:local IDscript "internet-detector"
:local LTERscript "LTE-reset"
:local IDRunning [/system script job find script=$IDscript]
:local LTER-running [/system script job find script=$LTERscript]
:log info message="checking if $IDscript "
:delay 1s
:if ([:len $LTER-running] = 1) 
	do={:log error message="LTE reset in progress, delaying start of internet-detector"
		}
		else={:if ([:len $IDRunning] = 0) 
				do={:log info message="starting internet-detector script"; /system script run $IDscript
					} \
			} \
		else={:log error message="internet-detector already running, delaying script start"
			}
second is the script to detect internet access using the fetch tool:
:log info message="internet-detector script starting" \
:local LTER-script "LTE-reset"
:local LTER-running
#remove old file before fetching new copy
:do {/file remove [find where name=Manual:TOC]; \
	/tool fetch url=https://update.mikrotik.com/wiki/Manual:TOC mode=https; \
#fetch website to check connectivity and look for timeout
#ping and resolve/nslookup do not fail when modem hangs
	on-error {:log error message="internet-detector first fail" \
	:delay 30s; \
#wait 30s and try again to give the modem room to self-recover
			:do {/tool fetch url=https://update.mikrotik.com/wiki/Manual:TOC mode=https \
				on-error {:log error message="internet-detector second fail" \
				#check if LTE-reset script is already running before starting it
				set $LTER-running = [system script job find script=$LTER-script] \
				:delay 1s;
				:if ([:len $LTER-running = 0]) do={/system script run $LTER-script} \
						}
				}
			}
	}
:log info message="internet-detector script finished" 
and finally is the script to reset LTE and/or ethernet interfaces as needed:
:log warning message="LTE-reset script starting"
#reset ether1 interface if its not running
while ([/interface ethernet get ether1 running] = false) \
do={:log error message="ether1 not running, resetting ethernet interface" \
	/interface ethernet 
		set ether1 disabled=yes \
		:delay 1s; \
		set ether1 disabled=no \
		:delay 15s; \
	}
:log warning message="Reseting LTE interface now"
/interface lte 
	set lte1 disabled=yes \
	:delay 3s; \
	set lte1 disabled=no
:log info message="LTE reset script finished"
I'll be really grateful for any help or critique I can get. I'm still learning and would like to learn how to do this right!
edit: had to fix my code blocks. I can provide a network diagram if needed.

Who is online

Users browsing this forum: No registered users and 26 guests