Need help to decode snmp string

N

Nicola Tiling

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=
 
B

Brian Candler

Nicola said:
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]
 
N

Nicola Tiling

Thanks! Life could be so easy ...


Am 23.05.2010 um 18:51 schrieb Brian Candler:
Nicola said:
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]
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top