hi...

using pysnmp, an 'snmp get' varBinds gives me an 'octet string' of values like:

1+1 Protection; East-West; Spiral Search; ODU Enable/Disable

I need to loop through these strings and see whether a particular string say, 'East-West' is present. I am not finding way to do this.

can this octet string be converted to some other form?

To see if an object can be converted to something else, you must find its class and look in the object's class documentation, so try this first:

print(type(obj))
print(obj.__class__)

thanks for the reply...but it's not helping me :(

thanks for the reply...but it's not helping me :(

What did python print ? If you don't know the python class of your 'octet string', you won't be able to convert it to anything.

@Grib....

Python gave me this:

<type 'instance'>
pysnmp.proto.rfc1902.OctetString

And

print('East-West ' in str(obj))

@tonyjv:


print 'East-West ' in str(obj) gave me False

@Grib....

Python gave me this:

<type 'instance'>
pysnmp.proto.rfc1902.OctetString

With this output, google led me to http://twistedsnmp.sourceforge.net/pydoc/pysnmp.proto.rfc1155.html#OctetString . The class has __len__ and __getitem__ methods. so you should try

# obj is your octet string object
for i in range(len(obj)):
  item = obj[i]
  print i, item
  print item.__class__

to see what the object contains. Please post the output here (in code tags).

There is also a method get(), you could try

value = obj.get()
print value
print value.__class__
<type 'str'>
22 W
<type 'str'>
23 e
<type 'str'>
24 s
<type 'str'>
25 t
<type 'str'>
26 ;
<type 'str'>
27
<type 'str'>
28 E
<type 'str'>
29 a
<type 'str'>
30 s
<type 'str'>
31 t
<type 'str'>
32 -
<type 'str'>
33 E
<type 'str'>
34 a
<type 'str'>
35 s
<type 'str'>
36 t
<type 'str'>
37 ;
<type 'str'>
38
<type 'str'>
39 S
<type 'str'>
40 p
<type 'str'>
41 i
<type 'str'>
42 r
<type 'str'>
43 a
<type 'str'>
44 l
<type 'str'>
45
<type 'str'>
46 S
<type 'str'>
47 e
<type 'str'>
48 a
<type 'str'>
49 r
<type 'str'>
50 c
<type 'str'>
51 h
<type 'str'>
52 ;
<type 'str'>
53
<type 'str'>
54 O
<type 'str'>
55 D
<type 'str'>
56 U
<type 'str'>
57
<type 'str'>
58 E
<type 'str'>
59 n
<type 'str'>
60 a
<type 'str'>
61 b
<type 'str'>
62 l
<type 'str'>
63 e
<type 'str'>
64 /
<type 'str'>
65 D
<type 'str'>
66 i
<type 'str'>
67 s
<type 'str'>
68 a
<type 'str'>
69 b
<type 'str'>
70 l
<type 'str'>
71 e
<type 'str'>
72 ;
<type 'str'>
73
<type 'str'>
74 M
<type 'str'>
75 a
<type 'str'>
76 n
<type 'str'>
77 u
<type 'str'>
78 a
<type 'str'>
79 l
<type 'str'>
80
<type 'str'>
81 F
<type 'str'>
82 r
<type 'str'>
83 e
<type 'str'>
84 q
<type 'str'>
85 u
<type 'str'>
86 e
<type 'str'>
87 n
<type 'str'>
88 c
<type 'str'>
89 y
<type 'str'>
90
<type 'str'>
91 E
<type 'str'>
92 n
<type 'str'>
93 t
<type 'str'>
94 r
<type 'str'>
95 y
<type 'str'>
96 ;
<type 'str'>
97
<type 'str'>
98 S
<type 'str'>
99 F
<type 'str'>
100 P
<type 'str'>
101
<type 'str'>
102 P
<type 'str'>
103 o
<type 'str'>
104 r
<type 'str'>
105 t
<type 'str'>
106 ;
<type 'str'>
107
<type 'str'>
108 A
<type 'str'>
109 d
<type 'str'>
110 a
<type 'str'>
111 p
<type 'str'>
112 t
<type 'str'>
113 i
<type 'str'>
114 v
<type 'str'>
115 e
<type 'str'>
116
<type 'str'>
117 C
<type 'str'>
118 o
<type 'str'>
119 d
<type 'str'>
120 i
<type 'str'>
121 n
<type 'str'>
122 g
<type 'str'>
123 /
<type 'str'>
124 M
<type 'str'>
125 o
<type 'str'>
126 d
<type 'str'>
127 u
<type 'str'>
128 l
<type 'str'>
129 a
<type 'str'>
130 t
<type 'str'>
131 i
<type 'str'>
132 o
<type 'str'>
133 n
<type 'str'>

I got this and I am not able to make anything out of it:(

k......i can see that every element of string say, 'west' is treated as a seperate string

@Grib........

value=varBinds[0][1].get()
AttributeError: OctetString instance has no attribute 'get'

@Grib........

value=varBinds[0][1].get()
AttributeError: OctetString instance has no attribute 'get'

Did you try

print dir(obj)

?

And simple

print (str(obj))
print(repr(obj))

output?

Output for

print dir(obj)

['_AbstractSimpleAsn1Item__hashedValue', '__add__', '__cmp__', '__doc__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__mul__', '__nonzero__', '__radd__', '__repr__', '__rmul__', '__str__', '_subtypeSpec', '_tagSet', '_value', '_verifySubtypeSpec', 'clone', 'defaultValue', 'getSubtypeSpec', 'getTagSet', 'getTypeMap', 'isSameTypeWith', 'isSuperTypeOf', 'prettyIn', 'prettyOut', 'prettyPrint', 'prettyPrinter', 'subtype', 'subtypeSpec', 'tagSet']

and for..

1.print (str(obj))
   2.print(repr(obj))

is..

Null('')

anything useful???

I found a solution :)

a=str(obj).split(';')
                for vals in a:
                        print vals

gives me:

1+1 Protection
 East-West
 East-East
 Spiral Search
 ............
 ...........

any better solutions are welcome...

I found a solution :)

a=str(obj).split(';')
                for vals in a:
                        print vals

gives me:

1+1 Protection
 East-West
 East-East
 Spiral Search
 ............
 ...........

any better solutions are welcome...

I think your solution is excellent, although I fail to see how str(obj) could print Null('') while str(obj).split(';') prints all this stuff :)

I am clueless too... thanx for all the inputs :)

Well, the __class__ helped me! Also, you can use .prettyPrint() on the object, store that into a string, then use normal string handling functions to search for your value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.