Variable not being referenced by ":find" command?

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?

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.

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"
}

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