Variables in a script

Hi,

I woud like to know if there is anyone that could help with this.

I would like to set the IP to an interface ie. ether1 from a variable but I am not sure how I would go about doing this…

Here is a simple example of the logic on how I am thinking. Is there a way to get it to work or am I on completely the wrong track here?

#Variables#
:local lanip
#Set variables#
:set lanip=192.168.10.1;
#Work#
/ip address add address $lanip

Thanks

Leon

#Variables#
:local lanip 192.168.10.1/24
#Work#
/ip address add address $lanip interface=ether1

Brilliant!!! Thanks!!!

And then for interest sake…

How would I modify lanip’s value later on in the scipt with a :set command?

:set lanip “1.2.3.4”

Note: there is no ‘=’ sign (like you used in the first message and there is no ‘$’ before the variable (lanip) you want to change. You can use another variable (with $ before its name) instead of the “1.2.3.4”. And you can calculate the value or get it by other script/mikrotik commands:

:set lanip (“1.2.” . “3.4”)
:set id [/file find name=“filename”]
:set iplen [:len $lanip]

Hi dada

I’ve seen the :len argument several times but can’t quite figure out what it does and the usermanual doesn’t help me either. Could you give me a short description?

Many thanks, G

Returns string length, like strlen() command in C.
Example:

[admin@MikroTik] > :put [:len "abcd"] 
4

Hi there

Thanks for the info! I thought it would mean “length”, but wasn’t 100% sure.

Where could I find a list of all such functions? I looked at the 2.9 manual but theres only a couple of functions.

I’m currently sitting with a problem where my variable isn’t always an integer and have no idea how to fix it. I wrote a loop to search for numbers in the string, but some characters in the variable causes the loop to stop from time to time. I think it could be an “integer devided by zero” error as it only happens to queues with no statistics.

My above script is meant to seperate the tx-upload-bytes and rx-upload-bytes from the “bytes” found in the queue. Perhaps you know of a better way than to loop through the integer, picking values until the value = “/”.

Many thanx, G

Can you provide that script?
I don’t see you posted it?

Hi Dragon

Are you looking for the full script?

I figured out that the problem was with the value of the variable actually being infinity. So I added this bit of code which seems to have fixed it completely:

:if ($bytestotal < 1000000000000000000) do={ } else={ :set bytestotal 0 }

It has to be part of you main loop. It checks to see if the variable is smaller than 1000GB, if it is, nothing will happen, if it’s not smaller than 1000GB, it is probably inifinite and therefore the “else” will set it to 0.

I hope it makes sense!
G

Good it’s fixed.

You can also write that as

:if ($bytestotal > 999999999999999999) do={ :set bytestotal 0 }

DUH!! Hitting myself against the head!

I honestly never thought about it that way!

I had a whole bunch of if’s inside of each other with log entried to try and figure out where the problem came from, and took out each if command as I know the problem wasn’t there. So I basically ended up with my thingy. Well, Maths was never my strong point!

Thanks for the tip! G

It wasn’t my strong point either, i barely pass it in the school.
But i like programming.

to get all the commands you can simply enter the start of the command and double tab in console, like:

in cousole you etner : (colon) and

[admin@Mikrotik] > :
environment  do       for      led    nothing  resolve  tobool  tonum   while  
terminal     error    foreach  len    parse    set      toid    tostr   
beep         execute  global   local  pick     time     toip    totime  
delay        find     if       log    put      toarray  toip6   typeof

and you get the list of available commands

Hi janisk

Thats how I’ve been doing it, but I was hoping to find an explination of what every command does too. Is there no other way to find it, or should I just guess and try?

Thanks for the info! G

type :confused:

and you will get command and description. Detailed description with examples is in manual.