SNMP oid error

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

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