Page 1 of 1

How to get "interface-name" by its' id ?

Posted: Sun Dec 27, 2009 6:49 pm
by Michael12345
I want to get "interface-name" through its' "id" by script,so call "id" is the figure in the title of "#" column in the following picutre.what is the parameter of the "#"? like "name" express "ether1_Lan", "type" express "ether", "MUT" express 1500,but what represent "#"? thanks

Re: How to get "interface-name" by its' id ?

Posted: Sun Dec 27, 2009 7:33 pm
by fewi
The question makes no sense. The ID is only known after 'print' commands and is only used in direct CLI commands, and never in scripts. In scripts it makes no sense to find the name by the ID, it only makes sense to find the ID by the name.

What are you trying to do?

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 5:49 am
by Michael12345
It's really ridiculous, "id" is so important, you bold to say "make no sense"?
1. I consider that one advantages compare with windows is Linux can be easily configured by script at will,not by "winbox" manually. Among other things,rules in Mikrotik is executed by it's order,so sequence is vital, if lack of this and you how to get rules' sequence? by name? only by "id" I think.

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 6:19 am
by fewi
Again - the IDs only matter in direct CLI commands and are only available after you've run a print command, it's the print command itself that generates the numerical ID in the column labeled '#'. They're a convenient shortcut that makes it easy for people to refer to things when manually entering commands. The real IDs that RouterOS uses to find items are not numerical and are not shown in the print command.

In scripts, you generate pointers to items by using find commands. In find commands the '#' column from print commands is not available because it is dynamically generated by the print command, the find command only can access all the other columns. In your original screenshot you show interfaces. If you wanted to manipulate the 'ether1_Lan' interface, you would find it by its name and either store it in a variable, or manipulate it directly. Below an example on how to change the MTU:
[admin@MikroTik] > /interface print where name=loopback
Flags: D - dynamic, X - disabled, R - running, S - slave 
 #     NAME                                                                                                                                                                                         TYPE             MTU   L2MTU
 6  R  loopback                                                                                                                                                                                     bridge           1500  65535
[admin@MikroTik] > :local x [/interface find name=loopback]; /interface set $x mtu=1480;
[admin@MikroTik] > /interface print where name=loopback                                 
Flags: D - dynamic, X - disabled, R - running, S - slave 
 #     NAME                                                                                                                                                                                         TYPE             MTU   L2MTU
 6  R  loopback                                                                                                                                                                                     bridge           1480  65535
No numerical ID needed.

You can refer to numerical IDs in scripts, but in that case they do not refer to a line with specific properties (that you can identify via find commands), but hard coded entries by item number. It's unnecessary to know the line number to refer to an item you can find by its properties.

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 11:08 am
by Michael12345
Thank your reply first. My intention is to code a script that can get the name about the existing router's Network Card and how many Network Card had been installed,can you tell me how to do can success that aim?
"ID" is vital, if not available now, add in to the next version would be wisely in my opinion.

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 11:37 am
by Michael12345
fewi: maybe I can achieve my goal by make a detour like what example you gave to make use by "MTU" etc, but that would foul up the code, and after a period of time, I maybe confuse by this segment of "make detour" code, or if this code be takeovered by other people(e.g successor),they would scarcely understand. In fact, "id" is not only in "interface" menu is needed, but also in all aspects to Mikrotik, and in every walk of life, where lacked of "sequence number"? So maybe "id" entry is not vital but absolutely necessary, not redundant at least.
"id" entry is main clue, I can follow the main clue to fetch any attribute of the entry,right?

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 7:11 pm
by fewi
Do you mean interfaces, or network cards?

Interfaces:
:local interfaces [/interface find]; :put ("There are " . [:len $interfaces] . " interfaces total"); :foreach interface in=$interfaces do={ :put ("Interface name: " . [/interface get $interface name]); };
That will, incidentally, print the interface names in the same order that a 'print' command would.

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 10:53 pm
by Chupaka
The ID is only known after 'print' commands and is only used in direct CLI commands, and never in scripts.
by the way, just checked with v4.2:
:log info [/interface get 12 name];
works both in CLI and as Script :shock:

Re: How to get "interface-name" by its' id ?

Posted: Mon Dec 28, 2009 10:59 pm
by fewi
Whoa. Used to be you had to run a 'print' command beforehand to generate that. Tested and it even works from a scheduled script or right after login.

Still, I don't know where you'd want to use an ID in a script rather than a find command to make damn sure you get the right item (you'd have to recode your scripts every time you change item order).

Re: How to get "interface-name" by its' id ?

Posted: Tue Dec 29, 2009 11:10 am
by janisk
ID field in CLI was introduced to make some things to work, name field should be used instead when ever possible.

also, to make ends meet - to differ what type of interfaces you have i would suggest to use /interface ethernet print and /interface wireless print etc.

Re: How to get "interface-name" by its' id ?

Posted: Tue Dec 29, 2009 4:01 pm
by Michael12345
Chupaka: you do me a great favor,you are so professional,thank you indeed.

Re: How to get "interface-name" by its' id ?

Posted: Wed Aug 08, 2012 8:59 am
by letabawireless
I would like to do something along these lines. I woud appreciate your help.

I want to do checks on most interfaces, which involves changing a gateway for each test. So I need an array of some sort, and use the names of the interfaces to do the test.

foreach i [/interface ethernet find name] and get

ether1
ether2
ether3
ether4

Any ideas ?

Re: How to get "interface-name" by its' id ?

Posted: Wed Aug 08, 2012 12:24 pm
by Chupaka
 :foreach i in=[/interface ethernet find] do={ :put [/interface ethernet get $i name]; }

Re: How to get "interface-name" by its' id ?

Posted: Wed Aug 08, 2012 12:53 pm
by letabawireless
Its a winner !!! Many thanks...... I bow to the masters....

Re: How to get "interface-name" by its' id ?

Posted: Fri Oct 24, 2014 10:45 am
by darkiesan
I want to print a subset of the inetrfaces via script, i.e. something like: "/interface print where name="ether*"

Unfortunately I havent yet found a way to use regexp or wildcards to achieve this. Any tips?

Re: How to get "interface-name" by its' id ?

Posted: Mon Oct 27, 2014 6:20 pm
by Chupaka
I want to print a subset of the inetrfaces via script, i.e. something like: "/interface print where name="ether*"
/interface print where name~"^ether"

Re: How to get "interface-name" by its' id ?

Posted: Sat Mar 10, 2018 9:20 am
by rushlife
I want to print a subset of the inetrfaces via script, i.e. something like: "/interface print where name="ether*"
/interface print where name~"^ether"

Hi Chupaka, I need write some script to rename ALL interfaces in router to DEFAULT-NAME.
Ex.: if some idiot renamed interface ether1 to eth00, I need script for rename this interface back to "ether1"....

Can somebody help me ? Thx folks....

Re: How to get "interface-name" by its' id ?

Posted: Sat Mar 10, 2018 12:58 pm
by sindy
Hi Chupaka, I need write some script to rename ALL interfaces in router to DEFAULT-NAME.
Ex.: if some idiot renamed interface ether1 to eth00, I need script for rename this interface back to "ether1"....
/interface
:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}

Re: How to get "interface-name" by its' id ?

Posted: Sat Mar 10, 2018 10:35 pm
by rushlife
Absolutely great. Thx.
Works like a charm.

How you guys learned this scripting ? I mean...training directly in ROS and debuging is awful...
How you doing that ?

Re: How to get "interface-name" by its' id ?

Posted: Sat Mar 10, 2018 10:48 pm
by sindy
How you guys learned this scripting ? I mean...training directly in ROS and debuging is awful...
How you doing that ?
It's like any other programming language, each has its own specifics. So I basically maintain some basic knowledge regarding what is possible in that language and what are its distinctive points, and then it is a matter of try and fail ;-)

I highly recommend using ssh as RouterOS uses coloring to warn you that something you type is not correct (yet). This is also true if you use WebFig but the colors are much more pale (vyblitý would be a more accurate expression ;-) ). And I use the [tab] key often to get context help.

I guess you may also talk to @jarda regarding a training.

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 7:09 am
by nichky
nice info

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 2:14 pm
by nichky
Hi Chupaka, I need write some script to rename ALL interfaces in router to DEFAULT-NAME.
Ex.: if some idiot renamed interface ether1 to eth00, I need script for rename this interface back to "ether1"....
/interface
:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}

did you tape correct that command, can you double check please?

Thanks

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 2:23 pm
by sindy
/interface
:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}

did you tape correct that command, can you double check please?

Thanks
Yes, I have debugged it on my router, what exactly goes wrong when you use it?

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 2:29 pm
by nichky
/interface
:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}

did you tape correct that command, can you double check please?

Thanks
Yes, I have debugged it on my router, what exactly goes wrong when you use it?

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 2:40 pm
by sindy
/interface
:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}

did you tape correct that command, can you double check please?

Thanks
Yes, I have debugged it on my router, what exactly goes wrong when you use it?
The first (highlighted ") happens to me if I type the command at / level, because there, the default-name has no meaning.

Can you enter "/interface" and try again from that level?

Or you may do that in a single line the following way:
/interface {:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}}

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 2:49 pm
by nichky
/interface
:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}

did you tape correct that command, can you double check please?

Thanks
Yes, I have debugged it on my router, what exactly goes wrong when you use it?
The first (highlighted ") happens to me if I type the command at / level, because there, the default-name has no meaning.

Can you enter "/interface" and try again from that level?

Or you may do that in a single line the following way:
/interface {:foreach counter=id in=[find default-name~"ether|wlan"] do={set $id name=[get $id value-name=default-name]}}

yeeeyeee..done

Thanks a lot

Re: How to get "interface-name" by its' id ?

Posted: Sun Mar 11, 2018 10:14 pm
by rushlife
How you guys learned this scripting ? I mean...training directly in ROS and debuging is awful...
How you doing that ?
It's like any other programming language, each has its own specifics. So I basically maintain some basic knowledge regarding what is possible in that language and what are its distinctive points, and then it is a matter of try and fail ;-)

I highly recommend using ssh as RouterOS uses coloring to warn you that something you type is not correct (yet). This is also true if you use WebFig but the colors are much more pale (vyblitý would be a more accurate expression ;-) ). And I use the [tab] key often to get context help.

I guess you may also talk to @jarda regarding a training.
thank you, you are just awesome

Re: How to get "interface-name" by its' id ?

Posted: Mon Mar 12, 2018 7:43 am
by Chupaka
Just a little remark: generally, I would first rename interfaces to "default_name.temporary_suffix", and then finally to "default_name". Because if, for example, ether1 was renamed to ether2 and ether2 was renamed to ether1 - the script will fail :)

Re: How to get "interface-name" by its' id ?

Posted: Mon Mar 12, 2018 10:39 pm
by rushlife
Just a little remark: generally, I would first rename interfaces to "default_name.temporary_suffix", and then finally to "default_name". Because if, for example, ether1 was renamed to ether2 and ether2 was renamed to ether1 - the script will fail :)
Nice tip, some example ? :-)

Re: How to get "interface-name" by its' id ?

Posted: Mon Mar 12, 2018 10:49 pm
by sindy
Just a little remark: generally, I would first rename interfaces to "default_name.temporary_suffix", and then finally to "default_name". Because if, for example, ether1 was renamed to ether2 and ether2 was renamed to ether1 - the script will fail :)
Nice tip, some example ? :-)
To si děláš srandu... the example is to simply use the same script twice, once translating the default names to a set of names which are guaranteed to differ from the existing ones (through using a suffix to the default name such as .befelemepeseveze, as @Chupaka has suggested), and then to run it once again with the final names. The Mikrotik scripting manual tells you how to concatenate strings so that you could append the suffix to the default names obtained using "get".

So as an exercise, try to modify the existing script to make it suitable for the first round.

If you wanted an example of why this may be necessary, @Chupaka has also provided it. If user-provided name of ether1 is ether2 and user-provided name of ether2 is ether1, the attempt to set user-provided name of ether1 to ether1 will fail because at that time the user-provided name of ether2 is also still ether1, and two ports cannot bear the same user-provided name.

Re: How to get "interface-name" by its' id ?

Posted: Fri Sep 13, 2019 5:34 pm
by hsabrey
you may try this
:global inName [/interface ethernet get number=0 name];
:put $inName