Need help to decode snmp string

Discussion in 'Ruby' started by Nicola Tiling, May 23, 2010.

  1. Hi

    I'm a ruby newbie and need a little help. I think I'm wrong with class =
    and instance methods? I can't figure out how to do a decode a snmp =
    output.

    I use the snmp library http://rubyforge.org/projects/snmplib/

    My script:

    #!/usr/local/bin/ruby18 -w
    require "rubygems"
    gem "snmp"
    require "snmp"

    SNMP::Manager.open:)Host =3D> '192.168.0.253', :Version =3D> :SNMPv1, =
    :Community =3D> 'public') do |manager|
    manager.load_module("RC-VLAN-MIB")
    48.times do |i|
    i =3D i+1
    response =3D manager.get(["rcVlanPortVlanIds.#{i}"])
    response.each_varbind do |varbind|
    p varbind.value
    end
    end
    end


    The output is like this

    "\000\001\000\004\000\005\000\006\000\f"
    "\000\001\000\f"
    "\000\001\000\002\000\005\000\006\000\a\000\b\000\v\000\f\000\r"
    "\000\001"

    But I want to decode this output to integer vlan id's.=20

    1,4,5,6,12
    1,12
    1,2,5,6,7,8,11,12,13
    1

    How can I do it with methodes from the snmp library? I'm playing with =
    decode and decode_value. But the only result was

    "undefined method `decode' for #<SNMP::VarBind:0x801fc7ca0> =
    (NoMethodError)"

    But "decode" is a (Public Class) methode of VarBind and I don't =
    understand what's wrong.

    http://snmplib.rubyforge.org/doc/index.html


    Also I don't understand why "f" stands for "12" and "v" for "11" and "r" =
    for "13"

    ruby says it is a octet string ("p varbind.value.asn1_type" shows "OCTET =
    STRING")

    With net-snmp I fetch vlan ids per port in hex

    snmpwalk -v 1 -c public -m RC-VLAN-MIB 192.168.0.253 =
    1.3.6.1.4.1.2272.1.3.3.1.3=20

    I get:

    RC-VLAN-MIB::rcVlanPortVlanIds.1 =3D Hex-STRING: 00 01 00 04 00 =
    05 00 06 00 0C=20
    RC-VLAN-MIB::rcVlanPortVlanIds.2 =3D Hex-STRING: 00 01 00 0C=20
    RC-VLAN-MIB::rcVlanPortVlanIds.3 =3D Hex-STRING: 00 01 00 02 00 =
    05 00 06 00 07 00 08 00 0B 00 0C=20


    Nicola=
    Nicola Tiling, May 23, 2010
    #1
    1. Advertising

  2. Nicola Tiling wrote:
    > The output is like this
    >
    > "\000\001\000\004\000\005\000\006\000\f"
    > "\000\001\000\f"
    > "\000\001\000\002\000\005\000\006\000\a\000\b\000\v\000\f\000\r"
    > "\000\001"
    >
    > But I want to decode this output to integer vlan id's.
    >
    > 1,4,5,6,12
    > 1,12
    > 1,2,5,6,7,8,11,12,13
    > 1
    >
    > How can I do it with methodes from the snmp library?


    I don't know much about the SNMP library. However:

    irb(main):002:0> "\000\001\000\004\000\005\000\006\000\f".unpack("n*")
    => [1, 4, 5, 6, 12]

    > Also I don't understand why "f" stands for "12" and "v" for "11" and "r"
    > for "13"


    \v is an escape for "vertical tab", \f is an escape for "form feed", and
    \r is "carriage return". These are ASCII characters with codes 11, 12
    and 13 respectively.

    irb(main):006:0> "\f"[0]
    => 12

    [ruby 1.8, use ord for ruby 1.9]

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, May 23, 2010
    #2
    1. Advertising

  3. Thanks! Life could be so easy ...


    Am 23.05.2010 um 18:51 schrieb Brian Candler:

    > Nicola Tiling wrote:
    >> The output is like this
    >>
    >> "\000\001\000\004\000\005\000\006\000\f"
    >> "\000\001\000\f"
    >> "\000\001\000\002\000\005\000\006\000\a\000\b\000\v\000\f\000\r"
    >> "\000\001"
    >>
    >> But I want to decode this output to integer vlan id's.
    >>
    >> 1,4,5,6,12
    >> 1,12
    >> 1,2,5,6,7,8,11,12,13
    >> 1
    >>
    >> How can I do it with methodes from the snmp library?

    >
    > I don't know much about the SNMP library. However:
    >
    > irb(main):002:0> "\000\001\000\004\000\005\000\006\000\f".unpack("n*")
    > => [1, 4, 5, 6, 12]
    >
    >> Also I don't understand why "f" stands for "12" and "v" for "11" and "r"
    >> for "13"

    >
    > \v is an escape for "vertical tab", \f is an escape for "form feed", and
    > \r is "carriage return". These are ASCII characters with codes 11, 12
    > and 13 respectively.
    >
    > irb(main):006:0> "\f"[0]
    > => 12
    >
    > [ruby 1.8, use ord for ruby 1.9]
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Nicola Tiling, May 25, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 01211983

    Agent SNMP (need help)

    01211983, Apr 14, 2009, in forum: Java
    Replies:
    1
    Views:
    410
    01211983
    Apr 15, 2009
  2. sumit
    Replies:
    0
    Views:
    344
    sumit
    Mar 10, 2012
  3. stewart dunn

    Newbie need help with Perl & SNMP

    stewart dunn, Sep 4, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    76
    stewart dunn
    Sep 4, 2003
  4. Eric Stanfield

    Need guidance on SNMP::translateObj

    Eric Stanfield, Jun 12, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    225
    Eric Stanfield
    Jun 12, 2004
  5. milaus

    SNMP module to query net-snmp

    milaus, Aug 31, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    388
    Dr.Ruud
    Aug 31, 2006
Loading...

Share This Page