Community discussions

MikroTik App
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

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

Sun Dec 27, 2009 6:49 pm

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
You do not have the required permissions to view the files attached to this post.
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

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

Sun Dec 27, 2009 7:33 pm

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?
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

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

Mon Dec 28, 2009 5:49 am

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.
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

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

Mon Dec 28, 2009 6:19 am

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.
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

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

Mon Dec 28, 2009 11:08 am

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.
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

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

Mon Dec 28, 2009 11:37 am

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?
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

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

Mon Dec 28, 2009 7:11 pm

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.
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

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

Mon Dec 28, 2009 10:53 pm

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:
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

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

Mon Dec 28, 2009 10:59 pm

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).
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

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

Tue Dec 29, 2009 11:10 am

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.
 
Michael12345
Member Candidate
Member Candidate
Topic Author
Posts: 139
Joined: Wed Oct 28, 2009 8:44 am

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

Tue Dec 29, 2009 4:01 pm

Chupaka: you do me a great favor,you are so professional,thank you indeed.
 
letabawireless
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Thu Jul 26, 2012 5:11 pm

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

Wed Aug 08, 2012 8:59 am

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 ?
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

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

Wed Aug 08, 2012 12:24 pm

 :foreach i in=[/interface ethernet find] do={ :put [/interface ethernet get $i name]; }
 
letabawireless
Frequent Visitor
Frequent Visitor
Posts: 63
Joined: Thu Jul 26, 2012 5:11 pm

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

Wed Aug 08, 2012 12:53 pm

Its a winner !!! Many thanks...... I bow to the masters....
 
User avatar
darkiesan
newbie
Posts: 42
Joined: Tue Nov 14, 2006 4:53 pm
Location: Stockholm, Sweden
Contact:

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

Fri Oct 24, 2014 10:45 am

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?
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

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

Mon Oct 27, 2014 6:20 pm

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"
 
User avatar
rushlife
Member Candidate
Member Candidate
Posts: 245
Joined: Thu Nov 05, 2015 12:30 pm

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

Sat Mar 10, 2018 9:20 am

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....
 
sindy
Forum Guru
Forum Guru
Posts: 10206
Joined: Mon Dec 04, 2017 9:19 pm

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

Sat Mar 10, 2018 12:58 pm

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]}
 
User avatar
rushlife
Member Candidate
Member Candidate
Posts: 245
Joined: Thu Nov 05, 2015 12:30 pm

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

Sat Mar 10, 2018 10:35 pm

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 ?
 
sindy
Forum Guru
Forum Guru
Posts: 10206
Joined: Mon Dec 04, 2017 9:19 pm

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

Sat Mar 10, 2018 10:48 pm

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.
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1281
Joined: Tue Jun 23, 2015 2:35 pm

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

Sun Mar 11, 2018 7:09 am

nice info
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1281
Joined: Tue Jun 23, 2015 2:35 pm

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

Sun Mar 11, 2018 2:14 pm

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
 
sindy
Forum Guru
Forum Guru
Posts: 10206
Joined: Mon Dec 04, 2017 9:19 pm

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

Sun Mar 11, 2018 2:23 pm

/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?
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1281
Joined: Tue Jun 23, 2015 2:35 pm

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

Sun Mar 11, 2018 2:29 pm

/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?
You do not have the required permissions to view the files attached to this post.
 
sindy
Forum Guru
Forum Guru
Posts: 10206
Joined: Mon Dec 04, 2017 9:19 pm

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

Sun Mar 11, 2018 2:40 pm

/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]}}
 
User avatar
nichky
Forum Guru
Forum Guru
Posts: 1281
Joined: Tue Jun 23, 2015 2:35 pm

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

Sun Mar 11, 2018 2:49 pm

/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
 
User avatar
rushlife
Member Candidate
Member Candidate
Posts: 245
Joined: Thu Nov 05, 2015 12:30 pm

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

Sun Mar 11, 2018 10:14 pm

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
 
User avatar
Chupaka
Forum Guru
Forum Guru
Posts: 8709
Joined: Mon Jun 19, 2006 11:15 pm
Location: Minsk, Belarus
Contact:

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

Mon Mar 12, 2018 7:43 am

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 :)
 
User avatar
rushlife
Member Candidate
Member Candidate
Posts: 245
Joined: Thu Nov 05, 2015 12:30 pm

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

Mon Mar 12, 2018 10:39 pm

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 ? :-)
 
sindy
Forum Guru
Forum Guru
Posts: 10206
Joined: Mon Dec 04, 2017 9:19 pm

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

Mon Mar 12, 2018 10:49 pm

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.
 
hsabrey
just joined
Posts: 21
Joined: Tue Jul 01, 2014 2:37 pm

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

Fri Sep 13, 2019 5:34 pm

you may try this
:global inName [/interface ethernet get number=0 name];
:put $inName

Who is online

Users browsing this forum: No registered users and 38 guests