SNMP oid error

J

jc

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
 
H

hallidave

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top