# Saving SNMP to variable

**URL:** <https://forum.mikrotik.com/t/saving-snmp-to-variable/144455>\
**Category:** Scripting\
**Created:** [November 6, 2020, 8:12pm UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455 "2020-11-06T20:12:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [November 6, 2020, 8:12pm UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455/1 "2020-11-06T20:12:21Z")

</div>

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:

```plaintext
/tool snmp-walk community=public version=2c address=127.0.0.1 oid=1.3.6.1.2.1.1

```

Tried this, but just give output to console.

```plaintext
{
:local test [/tool snmp-walk community=public version=2c address=127.0.0.1 oid=1.3.6.1.2.1.1]
:put $test
}

```

```plaintext
{
: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.

```plaintext
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 all **tools**

---

<div class="post-metadata">

**Author:** ![SiB](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/sib/32/1144_2.png) [@SiB](https://forum.mikrotik.com/u/SiB)\
**Post date:** [November 7, 2020, 1:21am UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455/2 "2020-11-07T01:21:43Z")

</div>

The same with reading lte cell-monitor feature…

Only way I know is via file… [http://forum.mikrotik.com/t/send-script-output-to-a-file/116250/1](http://forum.mikrotik.com/t/send-script-output-to-a-file/116250/1)

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![Jotne](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/jotne/32/56097_2.png) [@Jotne](https://forum.mikrotik.com/u/Jotne)\
**Post date:** [November 8, 2020, 9:50am UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455/3 "2020-11-08T09:50:01Z")

</div>

> [@](#):
>
> 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:

```plaintext
: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]

```

---

<div class="post-metadata">

**Author:** ![SiB](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/sib/32/1144_2.png) [@SiB](https://forum.mikrotik.com/u/SiB)\
**Post date:** [November 8, 2020, 2:17pm UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455/4 "2020-11-08T14:17:29Z")

</div>

> [@](#):
>
> 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

![](https://i.imgur.com/XLYXpwn.png)

---

<div class="post-metadata">

**Author:** ![teslasystems](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/teslasystems/32/57003_2.png) [@teslasystems](https://forum.mikrotik.com/u/teslasystems)\
**Post date:** [April 14, 2023, 3:53pm UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455/5 "2023-04-14T15:53:56Z")

</div>

If anyone interested, it’s now possible to get an oid value inside script by using **as-value** property. I don’t know in which ROS version it appeared, but in 7.8 it exists and works.

```plaintext
:local snmpvar [/tool snmp-get address=x.x.x.x oid=x.x.x... as-value]
:log info ($snmpvar->"value")

```

---

<div class="post-metadata">

**Author:** ![Amm0](https://forum.mikrotik.com/user_avatar/forum.mikrotik.com/amm0/32/1630_2.png) [@Amm0](https://forum.mikrotik.com/u/Amm0)\
**Post date:** [April 14, 2023, 4:24pm UTC](https://forum.mikrotik.com/t/saving-snmp-to-variable/144455/6 "2023-04-14T16:24:38Z")

</div>

I had a feature request in for this, and it was closed in late March, so it’s new.

```plaintext
: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=).

```plaintext
: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

```
