Community discussions

MikroTik App
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

How to get SNMP interface index in a script.

Mon Jun 22, 2020 10:54 am

I need the SNMP_IDX for each interface in Router OS.

This give me all interface
{
:foreach id in=[interface find] do={
	:local Name [interface get $id name] 
	:put "ifname=$Name"
}
}
But to get the SNMP Index I need to use print, there are no get. Value i need are the last digit for all ODI
:put [/interface print oid as-value ]
.id=*1;actual-mtu=.1.3.6.1.2.1.2.2.1.4.1;admin-status=.1.3.6.1.2.1.2.2.1.7.1;bytes-in=.1.3.6.1.2.1.31.1.1.1.6.1;bytes-out=.1.3.6.1
.2.1.31.1.1.1.10.1;comment=;discards-in=.1.3.6.1.2.1.2.2.1.13.1;discards-out=.1.3.6.1.2.1.2.2.1.19.1;errors-in=.1.3.6.1.2.1.2.2.1.
14.1;errors-out=.1.3.6.1.2.1.2.2.1.20.1;mac-address=.1.3.6.1.2.1.2.2.1.6.1;name=.1.3.6.1.2.1.2.2.1.2.1;oper-status=.1.3.6.1.2.1.2.
2.1.8.1;packets-in=.1.3.6.1.2.1.31.1.1.1.7.1;packets-out=.1.3.6.1.2.1.31.1.1.1.11.1;.id=*3;actual-mtu=.1.3.6.1.2.1.2.2.1.4.3;admin
-status=.1.3.6.1.2.1.2.2.1.7.3;bytes-in=.1.3.6.1.2.1.31.1.1.1.6.3;bytes-out=.1.3.6.1.2.1.31.1.1.1.10.3;comment=Remote Winbox conne
ction for FV155;discards-in=.1.3.6.1.2.1.2.2.1.13.3;discards-out=.1.3.6.1.2.1.2.2.1.19.3;errors-in=.1.3.6.1.2.1.2.2.1.14.3;errors-
out=.1.3.6.1.2.1.2.2.1.20.3;mac-address=.1.3.6.1.2.1.2.2.1.6.3;name=.1.3.6.1.2.1.2.2.1.2.3;oper-status=.1.3.6.1.2.1.2.2.1.8.3;pack
ets-in=.1.3.6.1.2.1.31.1.1.1.7.3;packets-out=.1.3.6.1.2.1.31.1.1.1.11.3;.id=*2;actual-mtu=.1.3.6.1.2.1.2.2.1.4.2;admin-status=.1.3
.6.1.2.1.2.2.1.7.2;bytes-in=.1.3.6.1.2.1.31.1.1.1.6.2;bytes-out=.1.3.6.1.2.1.31.1.1.1.10.2;comment=;discards-in=.1.3.6.1.2.1.2.2.1
.13.2;discards-out=.1.3.6.1.2.1.2.2.1.19.2;errors-in=.1.3.6.1.2.1.2.2.1.14.2;errors-out=.1.3.6.1.2.1.2.2.1.20.2;mac-address=.1.3.6
.1.2.1.2.2.1.6.2;name=.1.3.6.1.2.1.2.2.1.2.2;oper-status=.1.3.6.1.2.1.2.2.1.8.2;packets-in=.1.3.6.1.2.1.31.1.1.1.7.2;packets-out=.
1.3.6.1.2.1.31.1.1.1.11.2
In this case the SNMP idx = internal interface ID, but that is not the case normally. So how to tell from :put [/interface print oid as-value ] that I need interface with id *1, *2 etc? It seem not to work with loop.?

Code should be some like this:
{
:foreach id in=[/interface find] do={
	:local Name [/interface get $id name]
	:local Idx [/interface print oid as-value $id actual-mtu]
	:local SNMPIdx [ do some extract to get the last number in $Idx to get the actual SNMPIdx]
	:put "ifname=$Name SNMPIdx=$SNMPIdx"
}
}
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to get SNMP interface index in a script.

Tue Jun 23, 2020 11:16 am

Hmm. This seems to be hard to solve.
Rotuer OS do send interface id in all netflow packets, but there are no easy way to get hold of them, since OID only shows it on the print command and not on the get command.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10186
Joined: Mon Jun 08, 2015 12:09 pm

Re: How to get SNMP interface index in a script.

Tue Jun 23, 2020 11:49 am

Why would you need that internally to RouterOS? I mean, the SNMP tools are not that powerful that you would want to do SNMP things inside RouterOS scripting, or did I miss something?
When observing from the outside (an external system using SNMP to monitor a MikroTik device) of course you can walk the IF-MIB::ifName or IF-MIB::ifDescr table to obtain the mapping between interface and index. Tools like MRTG do that.
 
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to get SNMP interface index in a script.

Tue Jun 23, 2020 1:16 pm

If you look at my post over here you will see why. viewtopic.php?p=801674#p801674

Netflow packet do contain what interface they are sent trough. Name of the filed is inputSNMPidx or outputSNMPidx.
So I do know that with SNMP i can find the corresponding interface.
line	_time			src_ip		s_port	dest_ip		d_port	next_ip		byte		pacet	prot	in_if	out_if
1	2020-06-23 10:50:11.280	193.212.a.a	42744	92.220.b.b	514	10.10.10.50	3903	 	35	17	1	2	0
2	2020-06-23 10:50:00.570	193.212.a.a	22	92.220.b.b	55774	10.10.10.32	1312380	 	2191	6	1	2	24
3	2020-06-23 10:50:00.540	10.10.10.32	55774	193.212.a.a	22	92.220.200.1	1074672	 	9631	6	2	1	24
My goal is to get teh SNMPidx number and name sent to my Splunk server using Syslog. (I do already send a lot of stuff using syslog).
SNMP can not be used on routers behind NAT, and I like that all goes from Router to server and nothing do request to the router.
Since the list of interface changes when you add Vlan/VPN/bridges etc (Dynamic), I need to read this example once a day.
 
pe1chl
Forum Guru
Forum Guru
Posts: 10186
Joined: Mon Jun 08, 2015 12:09 pm

Re: How to get SNMP interface index in a script.

Tue Jun 23, 2020 4:24 pm

SNMP can not be used on routers behind NAT, and I like that all goes from Router to server and nothing do request to the router.
Ok that is why I do not consider that a problem, my networks do not use NAT...
 
ChrisCCC
just joined
Posts: 23
Joined: Thu Apr 07, 2016 7:28 pm

Re: How to get SNMP interface index in a script.

Fri Sep 17, 2021 9:56 pm

I know this is a relatively old post, but I've been wanting to be able to do this for a while, so have actually spent the time getting it working.

The following should get all your interfaces and output the interface name and OID in a CSV list.
:foreach i in=[/interface find] do={
    :local intName [get $i name]
    :local oidString [/interface print oid as-value where name=$intName]
    :local oid [:pick [:pick $oidString 0] 1]
    :local startVal
    :for i from=0 to=[:len $oid] do={
        :if ( [:pick $oid $i ($i+1)] = ".") do={:set startVal ($i+1)}
    }
    :set oid [:pick $oid $startVal [:len $oid]]
    :put ($intName . "," . $oid)
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to get SNMP interface index in a script.

Sat Sep 18, 2021 2:36 am

The script used as-is do error on ":local intName [get $i name]"
because "get from where"?


Rewrited Script:
/interface
:foreach item in=[find] do={
    :local intName [get $item name]
    :local intOID ([print oid as-value where name=$intName]->0->"name")
    :local intIdx [:pick $intOID 21 [:len $intOID]]
    :put "$intName,$intIdx"
}

Who is online

Users browsing this forum: greggio and 28 guests