# SNMP oid error

**URL:** <https://www.ruby-forum.com/t/snmp-oid-error/107691>\
**Category:** Ruby\
**Created:** [June 25, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/snmp-oid-error/107691 "2007-06-25T13:18:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 25, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/snmp-oid-error/107691/1 "2007-06-25T13:18:41Z")

</div>

Hi,

I’m using SNMP module into a simple script. I would like retrieve  
interfaces MAC  
addresses by using oid “1.3.6.1.2.1.2.2.1.6” :

- using snmpwalk it works fine

snmpwalk -v 1 -r 1 -t 2 -c public 127.0.0.1 .1.3.6.1.2.1.2.2.1.6  
IF-MIB::ifPhysAddress.1 = STRING: 0:10:83:fd:dd:67

- using ruby

ifTable\_columns = [“1.3.6.1.2.1.2.2.1.6”,]  
SNMP::Manager.open(:Host =\> “#{host}”, :Version =\> :SNMPv1, :Community  
=\> ‘public’) do |manager|  
manager.walk(ifTable\_columns) do |row|  
row.each { |vb| puts “#{vb.name.to\_s} #{vb.value.to\_s}  
#{vb.value.asn1\_type}”  
end  
end

1.3.6.1.2.1.2.2.1.6.1 Ã½Ãg OCTET STRING

It shows Ã½Ãg as MAC address. How can I fix it?

Thank you very much,  
Jc

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 25, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/snmp-oid-error/107691/2 "2007-06-25T15:08:03Z")

</div>

Hi,

The Ruby SNMP library isn’t as smart as Net-SNMP at formating output  
because it doesn’t have any knowledge of the underlying MIB. The  
actual format of the MAC address is just 6 bytes (octets) representing  
each digit of the MAC. Try vb.value.inspect to see what I mean. You  
need to do your own translation.

Try this instead of vb.value.to\_s:

vb.value.unpack(“H2H2H2H2H2H2”).join(":")

You could even add a to\_mac method to the OctetString class  
(untested).

module SNMP  
class OctetString  
def to\_mac  
raise “Invalid MAC” unless self.length == 6  
self.unpack(“H2H2H2H2H2H2”).join(":")  
end  
end  
end

Hope that helps.

Cheers,

Dave
