Community discussions

MikroTik App
 
DonMoelb
just joined
Topic Author
Posts: 3
Joined: Thu May 20, 2021 6:53 pm

Concatenate in Script

Thu May 20, 2021 7:48 pm

Hello everybody

I have a problem with an concatenate operation in a script that I can't understand nor solve.
To automatically set up VPN routers with specific IPs I wrote a script where I enter the specific IP and MAC segments into variables.
The variables then are used on different concatenate commands in the script.
I tested every command of the script one by one in the terminal (connected via Winbox) and it worked.
However, if I run the script one concatenate operation is not performed correctly.

It looks like this:
my variable declaration:
:global myIP
:global myMAC
:global myName
:set myIP "20.33"
:set myMAC "20:00:33"
:set myName "w20033"
The concatenate operation in the script that worked:

/interface ovpn-client
add name=ovpn comment=VPN mac-address=[ :put ("02:D7:5E:".$myMAC) ] \
connect-to=secret port=secret user=user1 profile=default-encryption \
certificate=[ :put ($myName.".ag") ] cipher=aes256

The concatenate operation that didn't work:
/ip address
add address=[:put ("172." . $myIP . ".1/24")] network=[:put ("172." . $myIP . ".0")] interface=ovpn

The output is:
172..1/24
invalid input for argument address

It ignores my variable in between the two strings.

As I said, if I copy paste the exact same codeline into the terminal and execute it, it works. The IPs 172.20.33.1/24 and 172.20.33.0 are added.
I then tried several variations of the code:
add address=[:put "172.$myIP.1/24"] network=[:put ("172.$myIP.0")] interface=ovpn
add address=[:put ("172.".$myIP.".1/24")] network=[:put ("172.".$myIP.".0")] interface=ovpn #no whitespace between "."
Both variations worked in the terminal but didn't if ran in a script.
I also tried nested :put operations within the :put operations but that didn't work in the terminal.

I have no idea anymore. How can it work in the terminal but not when ran in a script?
Any ideas?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Concatenate in Script  [SOLVED]

Fri May 21, 2021 1:53 pm

I have idea you do not know how is scripting, in general.
:put print on "video", do not pass any value to any variable or function "directly" on scripts.
(and network must NOT be used if you already put /24 on IP)

:global myIP "20.33"
:global myMAC "20:00:33"
:global myName "w20033"

/interface ovpn-client
add name=ovpn mac-address="02:D7:5E:$myMAC" certificate="$myName.ag" comment=VPN connect-to=secret port=secret user=user1 profile=default-encryption cipher=aes256

/ip address
add address="172.$myIP.1/24" interface=ovpn
 
DonMoelb
just joined
Topic Author
Posts: 3
Joined: Thu May 20, 2021 6:53 pm

Re: Concatenate in Script

Fri May 21, 2021 4:40 pm

Thanks for that.
I tried this and it works.

If you write "must not" be used, do you mean it can create problems or do you mean it's not necessary.

Yeah, I script and code in different languages but I'm pretty new to MikroTik scripts and the documentation is not really extensive and comprehensive so my code might not be top-notch.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Concatenate in Script

Fri May 21, 2021 5:06 pm

Thanks for that.
I tried this and it works.

If you write "must not" be used, do you mean it can create problems or do you mean it's not necessary.

Yeah, I script and code in different languages but I'm pretty new to MikroTik scripts and the documentation is not really extensive and comprehensive so my code might not be top-notch.
or you put /xx on IP and network is set accordingly
or
you put only IP without /xx and network

it's also valid set only IP as 170.2.3.5/255.255.255.0
 
DonMoelb
just joined
Topic Author
Posts: 3
Joined: Thu May 20, 2021 6:53 pm

Re: Concatenate in Script

Tue May 25, 2021 10:42 am

I see what you mean.
I shortened the command line now and leave the network out.
However, I found it's not a must to leave it out as it is just additional and doesn't cause a problems if the network is included
Our script is based on a MikroTik backup extended with a few command lines.
In this backup the command line is like i posted it:
add address=192.168.16.1/24 interface=bridgeLocal network=192.168.16.0
Hence the network can be omitted but doesn't need to be.

Who is online

Users browsing this forum: No registered users and 21 guests