Community discussions

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

Saving SNMP to variable

Fri Nov 06, 2020 10:12 pm

I do try to save output of Tool SNMP to a variable or send it to syslog. It does not seem to work:

I need to store/send to log, the the output of:
Code:Select all
/工具snmp-walk社区=公众中on=2c address=127.0.0.1 oid=1.3.6.1.2.1.1
Tried this, but just give output to console.
Code:Select all
{ :local test [/tool snmp-walk community=public version=2c address=127.0.0.1 oid=1.3.6.1.2.1.1] :put $test }
Code:Select all
{ :local test [/tool snmp-walk community=public version=2c address=127.0.0.1 oid=1.3.6.1.2.1.1] :log info message="$test" }
Mikrotik does log that I do run the SNMP command, but not output of it.
Code:Select all
snmp MikroTik: getnext 1.3.6.1.2.1.1.7.0 snmp MikroTik: getnext 1.3.6.1.2.1.1.6.0 snmp MikroTik: getnext 1.3.6.1.2.1.1.5.0 snmp MikroTik: getnext 1.3.6.1.2.1.1.4.0 snmp MikroTik: getnext 1.3.6.1.2.1.1.3.0 snmp MikroTik: getnext 1.3.6.1.2.1.1.2.0 snmp MikroTik: getnext 1.3.6.1.2.1.1.1.0 snmp MikroTik: getnext 1.3.6.1.2.1.1
If this does not work, please add this to later build.

PS from the manual:
Return value to a variable
Since RouterOS v6.43 it is possible to save the result of fetch command to a variable

So add it to alltools
Top
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1889
Joined: Sun Jan 06, 2013 11:19 pm
Location:Poland

Re: Saving SNMP to variable

Sat Nov 07, 2020 3:21 am

The same with reading lte cell-monitor feature...

Only way I know is via file...viewtopic.php?f=9&t=130448&hilit=execute#p819073
Code:Select all
execute "/tool snmp-walk community=public version=2c address=127.0.0.1 oid=1.3.6.1.2.1.1" file=out.txt file edit out.txt contents global out2 [file get out.txt contents] put [typeof $out2] str
Top
User avatar
Jotne
Forum Guru
Forum Guru
Topic Author
Posts: 3221
Joined: Sat Dec 24, 2016 11:17 am
Location:Magrathean

Re: Saving SNMP to variable

Sun Nov 08, 2020 11:50 am

Only way I know is via file...
Thanks for this suggestion.
I did send a request about this to MT Support SUP-33094
Having a lot of file write may wear out the flash?

Every command on RouterOS should be able to store data to all type of storage (Flash/Variable/log/syslog ++)

So to get it to syslog id did this:
Code:Select all
:execute "/tool snmp-walk community=public version=2c address=127.0.0.1 oid=1.3.6.1.2.1.1" file=out.txt :log info message=[file get out.txt contents]
Top
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1889
Joined: Sun Jan 06, 2013 11:19 pm
Location:Poland

Re: Saving SNMP to variable

Sun Nov 08, 2020 4:17 pm

Having a lot of file write may wear out the flash?
Every command on RouterOS should be able to store data to all type of storage (Flash/Variable/log/syslog ++)
I agree, and can only say at some netinstall RouterBoards we can write file into memory (ram disk, tempfs, etc.), not NAND
Image
Top
User avatar
teslasystems
刚刚加入了
Posts: 19
Joined: Sun Aug 09, 2015 3:00 pm

Re: Saving SNMP to variable

Fri Apr 14, 2023 6:53 pm

If anyone interested, it's now possible to get an oid value inside script by usingas-valueproperty. I don't know in which ROS version it appeared, but in 7.8 it exists and works.
Code:Select all
:local snmpvar [/tool snmp-get address=x.x.x.x oid=x.x.x... as-value] :log info ($snmpvar->"value")
Top
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 2372
Joined: Sun May 01, 2016 7:12 pm
Location:California

Re: Saving SNMP to variable

Fri Apr 14, 2023 7:24 pm

I had a feature request in for this, and it was closed in late March, so it's new.
Code:Select all
:put [/tool/snmp-get address=127.0.0.1 oid=1.3.6.1.4.1.14988.1.1.3.8.0 community=public as-value ] # oid=1.3.6.1.4.1.14988.1.1.3.8.0;type=integer;value=517

One note, the value returned seems to always be a string type (but reports the original SNMP type in type=).
Code:Select all
:put [:typeof ([/tool/snmp-get address=127.0.0.1 oid=1.3.6.1.4.1.14988.1.1.3.8.0 community=public as-value ]->"value") ] # str
Top

Who is online

Users browsing this forum: No registered users and 5 guests