Forum: Ruby SNMP oid error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A69f2a4366814b0e589b5d4895571d7c?d=identicon&s=25 jc (Guest)
on 2007-06-25 13:18
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
5f626a12840efa674efff81ae333602c?d=identicon&s=25 unknown (Guest)
on 2007-06-25 15:08
(Received via mailing list)
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
This topic is locked and can not be replied to.