merlinthemagic / MTM-RouterOS-Scripting

An on demand loaded library of functions for ROS scripting
GNU Lesser General Public License v3.0
21 stars 6 forks source link

query error: #1

Open gaborsinko opened 2 years ago

gaborsinko commented 2 years ago
/system script
add dont-require-permissions=no name=script1 owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="/import flash/MTM/Facts.rsc;\r\
    \n:global MtmFacts;\r\
    \n:local strTool [(\$MtmFacts->\"execute\") nsStr=\"getTools()->getStrings()\"];\r\
    \n\r\
    \n/ip arp print where \$dynamic=true and !complete and [(\$strTool->\"toLower\") \$interface] =[(\$strTool->\"toLower\") \"PublicDMZ\"]; #perfect\r\
    \n/ip dhcp-server lease print where host-name ~\"NurseSmurf.server\"; #perfect\r\
    \n/ip dhcp-server lease print where [(\$strTool->\"toLower\") host-name] ~[(\$strTool->\"toLower\") \"NurseSmurf.server\"]; #is null\r\
    \n"

why not return the last query result ... I guess it's not the Tool's fault but I can't figure out what the problem may be.

merlinthemagic commented 2 years ago

Hi,

I assume you have replaced a bunch of variables with static values before posting here. Else you have little use for the "toLower" function at all e.g. this works:

/import flash/MTM/Facts.rsc;
:global MtmFacts;
:local strTool [($MtmFacts->"execute") nsStr="getTools()->getStrings()"];

/ip arp print where $dynamic=true and !complete and $interface] = "publicdmz";
/ip dhcp-server lease print where host-name ~ "nursesmurf.server";

If this is the case, please post the full script next time as its easier to figure out whats going on.

Now the last line that is not working because you are expecting RouterOS scripting to know that you have wrapped the dhcp-lease attribute "host-name" in a function and you would like it to execute that function for each lease. That's not going to work, scripting in ROS does not have support for things like inline parameterized callbacks.

Instead you will need to iterate over the leases like so:


:foreach id in=[/ip dhcp-server lease find ] do={
    :if ([($strTool->"toLower") [/ip dhcp-server lease get $id host-name]] ~ "nursesmurf.server") do={
        :put [/ip dhcp-server lease get $id]
    }
}