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

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?
It’s really ridiculous, “id” is so important, you bold to say “make no sense”?
- 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.
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.
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.
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?
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.
by the way, just checked with v4.2:
:log info [/interface get 12 name];
works both in CLI and as Script ![]()
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).
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.
Chupaka: you do me a great favor,you are so professional,thank you indeed.
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 ?
:foreach i in=[/interface ethernet find] do={ :put [/interface ethernet get $i name]; }
Its a winner !!! Many thanks… I bow to the masters…
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?
/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…
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]}
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 ?
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.