Community discussions

MikroTik App
 
leonj
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Thu Jun 22, 2006 4:23 pm
Location: South Africa
Contact:

Variables in a script

Wed Mar 14, 2007 4:08 pm

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
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Wed Mar 14, 2007 5:08 pm

#Variables#
:local lanip 192.168.10.1/24
#Work#
/ip address add address $lanip interface=ether1
 
leonj
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 74
Joined: Thu Jun 22, 2006 4:23 pm
Location: South Africa
Contact:

Wed Mar 14, 2007 5:20 pm

Brilliant!!! Thanks!!!

And then for interest sake...

How would I modify lanip's value later on in the scipt with a :set command?
 
dada
Member Candidate
Member Candidate
Posts: 245
Joined: Tue Feb 21, 2006 1:44 pm

Wed Apr 11, 2007 5:53 pm

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]
 
User avatar
Giepie
Member
Member
Posts: 432
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Variables in a script

Sat Jun 21, 2008 2:22 am

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
 
User avatar
Dragonmen
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Thu Jun 16, 2005 6:20 pm
Location: Sabac, Serbia
Contact:

Re: Variables in a script

Sat Jun 21, 2008 2:01 pm

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
 
User avatar
Giepie
Member
Member
Posts: 432
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Variables in a script

Mon Jun 23, 2008 6:05 pm

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
 
User avatar
Dragonmen
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Thu Jun 16, 2005 6:20 pm
Location: Sabac, Serbia
Contact:

Re: Variables in a script

Tue Jun 24, 2008 9:52 am

Can you provide that script?
I don't see you posted it?
 
User avatar
Giepie
Member
Member
Posts: 432
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Variables in a script

Tue Jun 24, 2008 4:03 pm

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
 
User avatar
Dragonmen
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Thu Jun 16, 2005 6:20 pm
Location: Sabac, Serbia
Contact:

Re: Variables in a script

Wed Jun 25, 2008 10:07 am

Good it's fixed.

You can also write that as
:if ($bytestotal > 999999999999999999) do={ :set bytestotal 0 }
 
User avatar
Giepie
Member
Member
Posts: 432
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Variables in a script

Wed Jun 25, 2008 10:16 am

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
 
User avatar
Dragonmen
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Thu Jun 16, 2005 6:20 pm
Location: Sabac, Serbia
Contact:

Re: Variables in a script

Thu Jun 26, 2008 9:32 am

It wasn't my strong point either, i barely pass it in the school.
But i like programming.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Variables in a script

Thu Jun 26, 2008 10:28 am

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 <tab><tab>
[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
 
User avatar
Giepie
Member
Member
Posts: 432
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Variables in a script

Thu Jun 26, 2008 10:51 am

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
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7056
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Variables in a script

Thu Jun 26, 2008 12:52 pm

type :?

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

Who is online

Users browsing this forum: Amaan and 51 guests