Community discussions

MikroTik App
 
User avatar
k6ccc
Forum Guru
Forum Guru
Topic Author
Posts: 1490
Joined: Fri May 13, 2016 12:01 am
Location: Glendora, CA, USA (near Los Angeles)
Contact:

An equivalent of GoSub?

Thu Apr 15, 2021 8:48 pm

I fully admit that I don't use scripting much in RouterOS. I do have a few that either I directly used from this section of the forum, or took from here and modified for my own purposes. I barely can function on my own...

Does RouterOS scripting language have an equivalent of a GoSub command? I could not find one in the Wiki.

Here is what I am trying to accomplish. I have a script that does a ping test to a few addresses on my LAN, and sends an E-Mail to me if a device does not respond. I want to expand that to currently 34 devices. Rather than having to write (or copy, paste and modify) the same ping test routine 34 times, I would rather do something like this:
Load four variables that are used with error E-Mails
Load three variables for device #1
Execute a subroutine that performs the test, then return
Load three variables for device #2
Execute a subroutine that performs the test, then return
etc
Stop processing
At the end is the subroutine.

Here are a couple code segments of what I am currently doing (for just a few devices):
# Set up variables for average return times
:local AvgRtt;
# Set up variables for error E-Mails
:local Sub1 "Ping failure to"
:local Sub2 "from Router #1."
:local Bo1 "Test to IP: "
:local Bo2 "Average ping RTT: "

And the variables for two devices:
# Set the monitored IP address for test 01
:local IP01 192.168.106.51;
:local IP01n Wyze_camera-01_TBD;
:local Cam01Enable 1;

# Same for test 02
:local IP02 192.168.106.52;
:local IP02n Wyze_camera-02_Matrix-panels;
:local Cam02Enable 1;

And the test and E-Mail if needed:
# Start the test for Camera 01
:if ($Cam01Enable = 1) do={
/tool flood-ping $IP01 count=10 do={
:if ($sent = 10) do={
:set AvgRtt $"avg-rtt"
}}
:log info "Average RTT to $IP01 is:  $AvgRtt mSec"
:if ($AvgRtt = 0) do={
:log err "Wyze Ping failure to Camera 01 E-Mail is being sent";
# Send mail
/tool e-mail send to jim@redacted subject=("$Sub1 $IP01n $Sub2") body=("$Bo1 $IP01  $IP01n \n $Bo2 $AvgRtt mSec.")
}}
This last segment is what would become the SubRoutine (obviously with a little tweeking).
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: An equivalent of GoSub?  [SOLVED]

Fri Apr 16, 2021 8:05 am

 
User avatar
k6ccc
Forum Guru
Forum Guru
Topic Author
Posts: 1490
Joined: Fri May 13, 2016 12:01 am
Location: Glendora, CA, USA (near Los Angeles)
Contact:

Re: An equivalent of GoSub?

Fri Apr 16, 2021 8:23 am

Thanks Jotne. I'm gonna have to do some reading and playing with that when I'm awake (not enough for that right now). Maybe tomorrow...
 
User avatar
k6ccc
Forum Guru
Forum Guru
Topic Author
Posts: 1490
Joined: Fri May 13, 2016 12:01 am
Location: Glendora, CA, USA (near Los Angeles)
Contact:

Re: An equivalent of GoSub?

Tue Apr 20, 2021 2:58 am

Thanks Jotne. Took a little effect, but made it work for my simple purposes.

Who is online

Users browsing this forum: jaclaz and 20 guests