Community discussions

MikroTik App
 
el berto
Member Candidate
Member Candidate
Topic Author
Posts: 223
Joined: Wed Sep 26, 2007 10:53 am

How update/increase a variable?

Fri Apr 30, 2010 4:54 pm

Sorry, I can't find similar topic.

I have a variable
:global a 5;
and I would like increase/update by defined step (x example 5).
I used
:global a ($a + 2);
but now I have a = 2 and not 7.
How can I do?
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: How update/increase a variable?

Fri Apr 30, 2010 5:30 pm

[admin@MikroTik] > :local a 5; :set a ($a + 2); :put $a;
7
Use :global to bring the variable in initially, but then use :set to change its value. :global and :local just declare values, and set initial values.
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Fri Feb 10, 2023 11:55 pm

Hi
for this script i want updated new value to :local b



:local c "<TEST>"
:local b 50; :set b ($b + 4); :put [local $b];
:local d ($b+3)
/interface/wireguard set listen-port=($d+1) [find comment=$c];

i dont need see on terminal i want save it to script as new value please help me.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How update/increase a variable?

Sat Feb 11, 2023 12:48 pm

Hi
for this script i want updated new value to :local b

i dont need see on terminal i want save it to script as new value please help me.
Explain better, is not uderstandable
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sat Feb 11, 2023 2:45 pm

on below example :
:local a 5; :set a ($a + 2); :put $a;
result just show 7 0n terminal okay.
now i want result (ex.7) put on :local b so script automatic changed :
:local a 7; :set a ($a + 2); :put $a;
and every time scrip run : local b updated.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How update/increase a variable?

Sat Feb 11, 2023 2:54 pm

I not understand what you want do...
:global b
{
:local a 5
:set a ($a + 2)
:set b ($a + $b)

:put $a
:put $b
}
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sat Feb 11, 2023 6:07 pm

so thanks
this code work just for first run but if run it again :global b not changed i want every time that script run new value saved on "b" and updated.
for example:
:global b
{
:local a 5
:set a ($a + 2)
:set b ($a + $b)

:put $a
:put $b
}
when script run b = 7
and second run b = 9
and ...
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sat Feb 11, 2023 7:06 pm

I not understand what you want do...
:global b
{
:local a 5
:set a ($a + 2)
:set b ($a + $b)

:put $a
:put $b
}
i guest command :put just show result on terminal and cant update script yes?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How update/increase a variable?

Sat Feb 11, 2023 7:18 pm

I guess command :put just show result on terminal and cant update script yes?
Yeah, the :put shows it. But variables defined a :local disappear when they leave the scope. If you :global instead that might help until you understand scoping rules.
:global x 0
:set x ($x + 1)
:put $x
# shows: 1
:set x ($x + 1)
:put $x
# shows: 2
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sat Feb 11, 2023 7:31 pm

so thanks
actually i dont need see result.i want goal result saved on script.and every time script run value be updated.
for example when "b" on first is 5 and script run and "b" changed to 7 for new run script use 7 value for "b" and more ...
Last edited by BartoszP on Mon Feb 13, 2023 11:14 pm, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart, save network traffic
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How update/increase a variable?

Sat Feb 11, 2023 7:45 pm

saved on script.and every time script run value be updated
That's why you want your variable to be :global. Be aware it doesn't survive a reboot, variable are stored in memory.

So in example the $x will have that value for use elsewhere – no need to :put (print to console).

It's when use use "local", the variable may go "out of scope" (e.g. disappear), which may be confusion here. Parenthesis { } create a scope/block where it's set of local variables live – so as long as use modify AND use local variable INSIDE the { } grouping (e.g. in the same scope)... increment/update works totally fine with local too, just the variable will be lost when it hits the end of the code block e.g. "}"

Now since global variable exist across terminals/sessions (but not reboots), you do want to be careful with naming so global variables are unique. My $x is a poor example for using globals BTW – since if OTHER script was using $x, I'd be changing it for your example.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How update/increase a variable?

Sat Feb 11, 2023 7:53 pm

One more note...
The name of the global must be defined if you're using the variable access scripts or terminal.

This is explained here: https://wiki.mikrotik.com/wiki/Manual:S ... her_script
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sat Feb 11, 2023 8:07 pm

saved on script.and every time script run value be updated
That's why you want your variable to be :global. Be aware it doesn't survive a reboot, variable are stored in memory.

So in example the $x will have that value for use elsewhere – no need to :put (print to console).

It's when use use "local", the variable may go "out of scope" (e.g. disappear), which may be confusion here. Parenthesis { } create a scope/block where it's set of local variables live – so as long as use modify AND use local variable INSIDE the { } grouping (e.g. in the same scope)... increment/update works totally fine with local too, just the variable will be lost when it hits the end of the code block e.g. "}"

Now since global variable exist across terminals/sessions (but not reboots), you do want to be careful with naming so global variables are unique. My $x is a poor example for using globals BTW – since if OTHER script was using $x, I'd be changing it for your example.
many many thanks for your time.
really main problem is :
i use vps that never rebooted and i created Wire Guard tunnel.my country blocked this tunnel ports when i changed listen port WG run again now i cant changed it at any time i want change automatic listen port.i use this script when tunnel downed :
:local c "<myWG>"
:global b
{
:local a 501
:set a ($a + 2)
:set b ($a + $b)

:put $a
:put $b
}
/interface/wireguard set listen-port=$b [find comment=$c];
when tunnel gone script automatic change port to 503 after many time when tunnel again down script cant change listen port again.if possible help me.so thanks.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How update/increase a variable?

Sun Feb 12, 2023 6:56 pm

I think you should post your full config in a NEW post... But something like this would update the port by 2 every time it's run. I think it be better read the current value from the interface to know what your incrementing.
:global mywgname "<myWG>"
 {
  :local wginterface [/interface/wireguard/find comment=$mywgname]
  :local wgcurport [/interface/wireguard/get $wginterface listen-port]
  /interface/wireguard/set $wginterface listen-port=($wgcurport + 2)
}
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sun Feb 12, 2023 8:38 pm

so nice code but it just work for first run.in first run get listen port ex.119 and port is down and changed listen port to 121 after port up and 10 min passed and port again down port freezd on 121.
so thanks.
Last edited by BartoszP on Mon Feb 13, 2023 11:15 pm, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart, save network traffic
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How update/increase a variable?

Sun Feb 12, 2023 8:49 pm

It's hard to know without the full script or some context. I was just providing a rough example – I don't know enough about WG settings to know the code is even right to change this ;).

You add some logging to track what's going on:
:global mywgname "<myWG>"
 {
  :local wginterface [/interface/wireguard/find comment=$mywgname]
  :local wgcurport [/interface/wireguard/get $wginterface listen-port]
  /log info "about to update WG for $mywgname with id $wginterface using port $wgcurport"
  /interface/wireguard/set $wginterface listen-port=($wgcurport + 2)
  :delay 1s
  :set wgcurport [/interface/wireguard/get $wginterface listen-port]
  /log info "updated WG for $mywgname to use NEW port: $wgcurport"
}
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sun Feb 12, 2023 10:25 pm

actually you are best guy that spent your time for help to us really so thank you.
my country is A OK
my server is B OK
i cant make WG tuunel from A to B because WG blocked from my country to out ... so i changed plane and i make A to play server role for me so tunnel WG run!
listen port on local A is 80 and ping is charm.listen port on B not matter and can be any thing.but after hours passed tunnel down and i changed listen port on B location tunnel run again!
this loop repeated until i changed listen port on B location now i want this change make automatically with an scrip.your script work fine for first down and changed listen port and tunnel run again but when again tunnel downed script cant changed port again.i use your script on Netwach on down .
i hope that understand me .

so thanks.
:global mywgname "<myWG>"
{
:local wginterface [/interface/wireguard/find comment=$mywgname]
:local wgcurport [/interface/wireguard/get $wginterface listen-port]
/interface/wireguard/set $wginterface listen-port=($wgcurport + 2)
}
Last edited by BartoszP on Mon Feb 13, 2023 11:15 pm, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart, save network traffic
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How update/increase a variable?

Sun Feb 12, 2023 10:43 pm

@iking, you could use some time-based scheme, where you use the /system/clock. Like this which use 1000 + the minutes:
listen-port=(1000 +[:pick [/system/clock/get time] 3 5])
You'd want to use NTP (or /ip/cloud's set time) but then the port is predicable on both side without having to potentially reset the starting numbers.

Another approach be ZeroTier – really unsure how it work in your "national firewall" case... ZeroTier does a bunch of things automatically to deal with "firewalls", including changing ports (& communicating that to BOTH sides automatically). If both country A router and country B router were in the same ZeroTier network, you can then route over that connection. Whether the national firewall blocks communications to some of the internal ZeroTier "moons"/"roots", dunno. If more nodes on the "open firewall" side, it might be more robust and automatic to tunnel from the "restricted" side. So another thing to try, too. Certainly a bit more involved.
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Sun Feb 12, 2023 10:55 pm

my country so far from my router my router on Germany and don't block anything my country use china GFW .
when i create WG tunnel and add ip to 192.100.100.1/30 to Germany side and 192.100.100.2/30 to my country side ping come and is good.i create Netwch to check 192.100.100.2 is down on A location and put your script and set interval 00:00:10 for it.WG is run and is up after many time tunnel downed and netwch run script on down place listen port changed WG run again and work again after many time WG downd and now Netwach must run script again and changed port again but not work.exactly this is my ploblem.
so thanks.
Last edited by BartoszP on Mon Feb 13, 2023 11:15 pm, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart, save network traffic
 
iking
just joined
Posts: 17
Joined: Fri Feb 19, 2016 8:57 pm

Re: How update/increase a variable?

Mon Feb 13, 2023 4:29 pm

i know a new think !
when disable netwach and enabled again script worked and changed listen-port so i made a little change :


:global mywgname "<MyWG>"
{
:local wginterface [/interface/wireguard/find comment=$mywgname]
:local wgcurport [/interface/wireguard/get $wginterface listen-port]
/interface/wireguard/set $wginterface listen-port=($wgcurport + 2)
}
{
/tool/netwatch/disable [find comment="<MyWG>"]
:delay 10s
/tool/netwatch/enable [find comment="<MyWG>"]
:delay 5s
}

do you have any better idea ?

Who is online

Users browsing this forum: No registered users and 21 guests