Community discussions

MikroTik App
 
mmorelie
just joined
Topic Author
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Array modification

Fri Apr 06, 2018 3:34 pm

I try to create a script that get information from the configuration of the router or from some commands and store the result in an array.

I have declare some pppoe-client links and want to know the status of each one and save it in different array, one per pppoe-client link.
:global LD1 {user="" ; interface="ether1"; operstatus="" ; admstatus=""; upcounter=0}
:global LM1 {user="" ; interface="ether1"; operstatus="" ; admstatus=""; upcounter=0}
:global LDV1 {user="" ; interface="ether1"; operstatus="" ; admstatus=""; upcounter=0}
:global LV2 {user="" ; interface="ether1"; operstatus="" ; admstatus=""; upcounter=0}
:global LM2 {user="" ; interface="ether1"; operstatus="" ; admstatus=""; upcounter=0}
:global LDV2 {user="" ; interface="ether1"; operstatus="" ; admstatus=""; upcounter=0}

:global LOGIN {"LD1"; "LM1"; "LDV1"; "LV2"; "LM2"; "LDV2"}
I have create a loop to parse every pppoe-cleint link :
:foreach i in $LOGIN do={:interface pppoe-client monitor $i once do={:set ($i->"operstatus") $status}}
But the result is not what i want :
 environment print 
LD1={admstatus=""; interface="ether1"; operstatus=""; upcounter=1; user=""}
LDV1={admstatus=""; interface="ether1"; operstatus=""; upcounter=0; user=""}
LDV2={admstatus=""; interface="ether1"; operstatus=""; upcounter=0; user=""}
LM1={admstatus=""; interface="ether1"; operstatus=""; upcounter=0; user=""}
LM2={admstatus=""; interface="ether1"; operstatus=""; upcounter=0; user=""}
LOGIN={"LD1"; "LM1"; "LDV1"; "LV2"; "LM2"; "LDV2"}
LV2={admstatus=""; interface="ether1"; operstatus=""; upcounter=0; user=""}
what is wrong ?
 
pe1chl
Forum Guru
Forum Guru
Posts: 6913
Joined: Mon Jun 08, 2015 12:09 pm

Re: Array modification

Fri Apr 06, 2018 4:28 pm

Impossible to know when it is not clear what would be the expected result...
However, I feel with you. Debugging RouterOS scripting is extremely tricky and it often behaves different from what you expect.
What I learned the hard way is to always break up things in small statements. The expression evaluator is very primitive.
However, I don't know how to apply that to your problem.
 
mmorelie
just joined
Topic Author
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Re: Array modification

Fri Apr 06, 2018 4:48 pm

the result as to be for exemple :
LD1={admstatus=""; interface="ether1"; operstatus="connected"; upcounter=1; user=""}
 
pe1chl
Forum Guru
Forum Guru
Posts: 6913
Joined: Mon Jun 08, 2015 12:09 pm

Re: Array modification

Fri Apr 06, 2018 4:57 pm

why?
the result is set to $status. is $status set to "connected" outside of your example?
 
mmorelie
just joined
Topic Author
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Re: Array modification

Fri Apr 06, 2018 5:03 pm

no $status is not fixed, it's read from the command :
:interface pppoe-client monitor $i once
it can be connected or disconnected or something else
 
pe1chl
Forum Guru
Forum Guru
Posts: 6913
Joined: Mon Jun 08, 2015 12:09 pm

Re: Array modification

Fri Apr 06, 2018 5:28 pm

But where is that transferred to $status? I don't see that anywhere.
I think you need to:
:set $status [:interface pppoe....]
and then you can set the array variable to $status.
 
mmorelie
just joined
Topic Author
Posts: 18
Joined: Thu Jul 28, 2016 4:04 pm

Re: Array modification

Fri Apr 06, 2018 6:18 pm

it's done with this line

:foreach i in $LOGIN do={:interface pppoe-client monitor $i once do={:set ($i->"operstatus") $status}}

when you do :interface pppoe-client monitor LD1 once do={:set (LD1->"operstatus") $status

the actual status is put in the array LD1, at operstatus variable value.

I want this for each one of my value in array LOGIN
 
pe1chl
Forum Guru
Forum Guru
Posts: 6913
Joined: Mon Jun 08, 2015 12:09 pm

Re: Array modification

Fri Apr 06, 2018 7:43 pm

As I wrote earlier, the expression evaluator is very primitive.
Separate what you want to do in some steps.
First put the status in a variable, then put the variable value in the array.
But I don't understand this "monitor" command and the relation to the $status variable, so I could be wrong.

Who is online

Users browsing this forum: msatter and 14 guests