Community discussions

MikroTik App
 
myki
just joined
Topic Author
Posts: 5
Joined: Mon Jun 13, 2022 6:27 am

The issue of a function containing variables unsuccessfully.

Sat Jun 01, 2024 9:05 am

# Define the global function
:global DNSStatus do={
    :local actionValue $1
    /ip firewall nat $actionValue [find comment~"hack_dns"];
}

# Set the action value and call the function
:global DNSAction "disable"
$DNSStatus $DNSAction
 
msatter
Forum Guru
Forum Guru
Posts: 2942
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: The issue of a function containing variables unsuccessfully.

Sat Jun 01, 2024 10:47 am

Then an other question is, what is the meaning of life?
 
myki
just joined
Topic Author
Posts: 5
Joined: Mon Jun 13, 2022 6:27 am

Re: The issue of a function containing variables unsuccessfully.

Sat Jun 01, 2024 11:30 am

:global DNSStatus do={
    :local actionValue $1
    /ip firewall nat $actionValue [find comment~"hack_dns"];
}

# Set the action value and call the function
:global DNSAction "disable"

:global Pong

{
    #:local DNSStatus $DNSStatus;

    :if ($DNSStatus = "up") do={
       :global DNSAction "enable"
    }
    :if ($DNSStatus = "down") do={
        :global DNSAction  actionValue ="disable"
    }
    :if (($DNSStatus != "up") && ($DNSStatus != "down")) do={
        log info "Unknown DNS status: $DNSStatus"
    }
}
 /tool netwatch
add comment=hack_dns disabled=no down-script=":global Pong \$status\r\
    \n/system/script/run hack_dns\r\
    \n:log info \"\$Pong\"" host=192.168.1.2 http-codes="" name=hack_dns \
    test-script="" type=simple up-script=":global Pong \$status\r\
    \n/system/script/run hack_dns\r\
    \n:log info \"\$Pong \""
This achieves code reuse.
Make the code more concise and only need to take the modification function when modifying
 
msatter
Forum Guru
Forum Guru
Posts: 2942
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: The issue of a function containing variables unsuccessfully.

Sat Jun 01, 2024 1:24 pm

My take on this and this is not tested:
:global DNSstatus do={
    /ip firewall nat $1 [find where comment~"hack_dns"];
}

:global Pong do={
    :if ($1 = "up") do={
       	:global DNSstatus enable
       	:log info "DNS status: $1"
       	:return
    }
    :if ($1 = "down") do={
        :global DNSstatus disable
        :log info "DNS status: $1"
        :return
    }
    :log info "DNS status: unkown"
}

 /tool netwatch
add comment=hack_dns disabled=no down-script=":global Pong \$status\r\
    \n/system/script/run hack_dns\r\
    \n:log info \"\$Pong\"" host=192.168.1.2 http-codes="" name=hack_dns \
    test-script="" type=simple up-script=":global Pong \$status\r\
    \n/system/script/run hack_dns\r\
    \n:log info \"\$Pong \""
 
myki
just joined
Topic Author
Posts: 5
Joined: Mon Jun 13, 2022 6:27 am

Re: The issue of a function containing variables unsuccessfully.

Sat Jun 01, 2024 3:13 pm

Cannot take effect
 
msatter
Forum Guru
Forum Guru
Posts: 2942
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: The issue of a function containing variables unsuccessfully.

Sat Jun 01, 2024 9:20 pm

Then we return the question of what the meaning of life is?
 
jaclaz
Forum Guru
Forum Guru
Posts: 2151
Joined: Tue Oct 03, 2023 4:21 pm

Re: The issue of a function containing variables unsuccessfully.

Sun Jun 02, 2024 12:05 pm

Ray Bradbury:
Life is trying things to see if they work.
 
optio
Forum Veteran
Forum Veteran
Posts: 962
Joined: Mon Dec 26, 2022 2:57 pm

Re: The issue of a function containing variables unsuccessfully.

Sun Jun 02, 2024 2:20 pm

Command and arguments cannot be builded from variables, build command as string and use :execute command to execute it.
:local actionValue disable
:execute "/ip firewall nat $actionValue [find comment~\"hack_dns\"]" as-string
 
msatter
Forum Guru
Forum Guru
Posts: 2942
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: The issue of a function containing variables unsuccessfully.

Sun Jun 02, 2024 3:07 pm

Ray Bradbury:
Life is trying things to see if they work.
Then, what if nothing succeeds? Do you then know the meaning is of life!? ;)

Thanks Optio.
 
myki
just joined
Topic Author
Posts: 5
Joined: Mon Jun 13, 2022 6:27 am

Re: The issue of a function containing variables unsuccessfully.

Sun Jun 02, 2024 3:35 pm

Command and arguments cannot be builded from variables, build command as string and use :execute command to execute it.
:local actionValue disable
:execute "/ip firewall nat $actionValue [find comment~\"hack_dns\"]" as-string

thank you for your help!
i found solution
:global FirewallNatAction do={
  :global actionValue;
  :set actionValue $actionValue;
  :put $actionValue
  :execute "/ip firewall nat $actionValue [find comment~\"hack_dns\"]" as-string
}
$FirewallNatAction actionValue=enable
 
msatter
Forum Guru
Forum Guru
Posts: 2942
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: The issue of a function containing variables unsuccessfully.

Sun Jun 02, 2024 7:28 pm

:global FirewallNatAction do={
:global actionValue;
:set actionValue $actionValue;

:put $actionValue
:execute "/ip firewall nat $actionValue [find comment~\"hack_dns\"]" as-string
}
$FirewallNatAction actionValue=enable
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12632
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: The issue of a function containing variables unsuccessfully.

Tue Jun 04, 2024 3:00 am


:global FirewallNatAction do={
:global actionValue;
:set actionValue $actionValue;
:put $actionValue
:execute "/ip firewall nat $actionValue [find comment~\"hack_dns\"]" as-string
}
$FirewallNatAction actionValue=enable


/ip firewall nat disable [find where comment~"hack_dns"]

:global FirewallNatAction do={
  :execute "/ip firewall nat $1 [find where comment~\"hack_dns\"]"
}
$FirewallNatAction enable

Or disable/enable
/ip firewall nat find [set [find where comment~"hack_dns"] disabled=(!($disabled))]
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4441
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: The issue of a function containing variables unsuccessfully.

Tue Jun 04, 2024 3:34 am

Or do NOT use the "enable" and "disable" commands, use the disable=yes|no attribute with a "set". Why? Commands cannot be variables, but attributes can.

This avoids the ugly [:execute] & the related issues with that like escaping, creating new subshell/etc., lack of syntax highlights, etc):
:global DisableNatDns do={ /ip/firewall/nat set [find comment~"hack_dns"] disabled=$1 }
$DisableNatDns yes
$DisableNatDns no
Last edited by Amm0 on Tue Jun 04, 2024 7:04 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12632
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: The issue of a function containing variables unsuccessfully.

Tue Jun 04, 2024 3:42 am

missing something and/or too much "/"....
:global DisableNatDns do={/ip firewall nat set [find where comment~"hack_dns"] disabled=$1}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4441
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: The issue of a function containing variables unsuccessfully.

Tue Jun 04, 2024 3:50 am

Geez, "yes" and "no"
NOT "on" and "off" — which I fixed from original example

Or... "where" — I'm not sure that's needed since there is only one attribute to be searched;.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12632
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: The issue of a function containing variables unsuccessfully.

Tue Jun 04, 2024 10:53 am

the d on disabled ;)