Community discussions

MikroTik App
 
S8T8
Member Candidate
Member Candidate
Topic Author
Posts: 126
Joined: Thu Sep 15, 2022 7:15 pm

Get a variable from a variable?

Fri Sep 13, 2024 4:18 am

Dear experts,
sorry, I'm very very poor knowledge of scripting...

I was able to add my IP in a global variable, like:
:global WAN1 "100.10.20.40"
:global WAN2 "100.10.20.50"

it would be nice to achieve something like:
:log info "WAN1 IP is 100.10.20.40 and WAN2 IP is 100.10.20.50"

goal is to "get" the WAN name to set $WAN1/2 , like $[$[/interface get [find name=WAN1] name]]

Is this possible?

Hope this is understandable :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12390
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Get a variable from a variable?

Fri Sep 13, 2024 10:39 am

is a nonsense
[/interface get [find name=WAN1] name]
everytime, if WAN1 exist, the result is WAN1

goal is to "get" the WAN name to set $WAN1/2
If you must insert WAN1 or WAN2 on the script, are already WAN1 and WAN2...

Be more clear.
 
S8T8
Member Candidate
Member Candidate
Topic Author
Posts: 126
Joined: Thu Sep 15, 2022 7:15 pm

Re: Get a variable from a variable?

Fri Sep 13, 2024 12:47 pm

Right, it was not very accurate...

1) In the DHCP up script I get the interface and address + create a global variable:
:local WANif [/interface get $"interface" name]
:local WANip [:tostr $"lease-address"]
[:parse ":global $WANif $WANip"] <- THANKS to REXTENDED for this!
:put $WAN1 -> 100.10.20.40

2) In the DHCP down, I know the interface name but not the previous IP:
:local WANif [/interface get $"interface" name]
:log warning "Interface down, IP 100.10.20.40 is not available" <- to retrieve IP from interface name, $WANif -> WAN1 or WAN2 , then with the WAN name i should "call" the global variable $WAN1 -> 100.10.20.40, something like [$[$WANif]]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12390
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Get a variable from a variable?  [SOLVED]

Fri Sep 13, 2024 6:53 pm

[:parse ":global $WANif $WANip"] <- THANKS to REXTENDED for this!
Thanks

***********************************


Bad approach.

Simply create one global array with inside interface / ip pair "WANx=o.l.d.ip;WANy=an.othe.r.ip",
is more easy and intuitive.

untested concept code

# WRITE
:global globalvarname

:local WANif [/interface get $"interface" name]
:local WANip [:tostr $"lease-address"]

:set ($globalvarname->$WANif) $WANip


# READ
:global globalvarname

:local WANif [/interface get $"interface" name]
:local previousIP ($globalvarname->$WANif)

EXTRA: Consider that one interface can have more than on IP, and get do not support arrays as results...
 
S8T8
Member Candidate
Member Candidate
Topic Author
Posts: 126
Joined: Thu Sep 15, 2022 7:15 pm

Re: Get a variable from a variable?

Tue Sep 17, 2024 5:54 pm

Thanks again @rextended, you're so clever!
I wasn't that good integrating what I was trying to achieve, in the end, using :if ($WANif=WAN1) do= else=, I got a variable of WAN1IP WAN1=100.10.20.40 which is fine, it's just for hobby.

MikroTik community owe you a lot!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12390
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Get a variable from a variable?

Tue Sep 17, 2024 6:20 pm

Thank you, but remember that you too are a piece that everyone needs.

Who is online

Users browsing this forum: No registered users and 10 guests