Community discussions

MikroTik App
 
User avatar
ghostinthenet
newbie
Topic Author
Posts: 31
Joined: Sun Apr 04, 2021 1:36 pm
Location: Niagara-on-the-Lake, Canada
Contact:

Variable not being referenced by ":find" command?

Wed Jan 17, 2024 9:40 pm

I'm probably too deep in the weeds and am missing something obvious here, but I'm running a script to determine the current outbound interface and can't get the find command to pull data from a variable.

/ip route \
:local immediateGateway [get [find where 8.8.8.8 in dst-address and active and routing-table=main] value-name=immediate-gw] \
:put $immediateGateway \
:put ("Raw: ". [:pick $immediateGateway ([:find $immediateGateway "%" -1] + 1) 64]) \
:put ("Quoted: ".[:pick "$immediateGateway" ([:find "$immediateGateway" "%" -1] + 1) 64]) \
:put ("Value: ".[:pick "203.0. 113.1%interfaceWan1" ([:find "203.0. 113.1%interfaceWan1" "%" -1] + 1) 64])

Output Is as follows.

203.0. 113.1%interfaceWan1
Raw:
Quoted:
Value: interfaceWan1

So... the immediateGateway local variable gets assigned, but I can't get the pick/find to pull from the value. I've tried putting the variable in raw, putting it in quoted, and doing the pick/find with the actual value itself. Doing the pick/find seems to work with the value of the variable, but I'm having no luck with getting find command to pick up the variable itself.

Thoughts?
 
User avatar
vingjfg
Member Candidate
Member Candidate
Posts: 291
Joined: Fri Oct 20, 2023 1:45 pm

Re: Variable not being referenced by ":find" command?  [SOLVED]

Wed Jan 17, 2024 10:18 pm

Hi @ghostinthenet,

I got it working - in my case the issue was that the variable immediateGateway was an array. Here is my code:
{
:local immediateGateway [/ip/route get [/ip route find where 8.8.8.8 in dst-address and active and routing-table=main] value-name=immediate-gw] 
:put [:typeof $immediateGateway]
:local immgwstr [:pick $immediateGateway 0]
:put ("Where:" . [:tostr [:find $immgwstr "%" 0]])
:put ("Raw: ". [:pick $immgwstr ([:find $immgwstr "%" -1] + 1) 64])
}
And the execution:
{
{... :local immediateGateway [/ip/route get [/ip route find where 8.8.8.8 in dst-address and active and routing-table=main] value-name=immediate-gw] 
{... :put [:typeof $immediateGateway]
{... :local immgwstr [:pick $immediateGateway 0]
{... :put ("Where:" . [:tostr [:find $immgwstr "%" 0]])
{... :put ("Raw: ". [:pick $immgwstr ([:find $immgwstr "%" -1] + 1) 64])
{... }
array
Where:10
Raw: vlan.4000
Hope that it helps.
 
optio
Long time Member
Long time Member
Posts: 675
Joined: Mon Dec 26, 2022 2:57 pm

Re: Variable not being referenced by ":find" command?

Wed Jan 17, 2024 10:24 pm

You can have multiple routes per IP, you can iterate find like this:
/ip/route
:foreach routeId in=[find where 8.8.8.8 in dst-address and active and routing-table=main] do={
  :local immediateGateway ([get $routeId]->"immediate-gw")
  :put "immediateGateway: $immediateGateway"
}
 
User avatar
ghostinthenet
newbie
Topic Author
Posts: 31
Joined: Sun Apr 04, 2021 1:36 pm
Location: Niagara-on-the-Lake, Canada
Contact:

Re: Variable not being referenced by ":find" command?

Wed Jan 17, 2024 10:53 pm

Of course it’s an array. Predictably, I feel stupid for missing that. Thanks!

Who is online

Users browsing this forum: No registered users and 9 guests