Cant perform SNMP_SET via PyASN1 modules

127 Views Asked by At

I am trying to perform simple snmp-set request with following code:

import socket
import sys

from pyasn1.codec.ber import decoder
from pyasn1.codec.ber import encoder

from pyasn1_modules import rfc1155
from pyasn1_modules import rfc1157
from pyasn1.type import univ

SNMP_GET = 0
SNMP_SET = 3

msg = rfc1157.Message()
msg.setComponentByPosition(0)
msg.setComponentByPosition(1, sys.argv[1]) # Set community
# pdu
pdus = msg.setComponentByPosition(2).getComponentByPosition(2)
pdu = pdus.setComponentByPosition(SNMP_SET).getComponentByPosition(SNMP_SET)
pdu.setComponentByPosition(0, 123)
pdu.setComponentByPosition(1, 0)
pdu.setComponentByPosition(2, 0)
vbl = pdu.setComponentByPosition(3).getComponentByPosition(3)
vb = vbl.setComponentByPosition(0).getComponentByPosition(0)
vb.setComponentByPosition(0, sys.argv[3]) # Set OID

vb.setComponentByPosition(1).getComponentByPosition(1).setComponentByPosition(0).getComponentByPosition(0).setComponentByPosition(1, univ.OctetString('testvalue')).getComponentByPosition(1) # Set value

print('sending: %s' % msg.prettyPrint())

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(encoder.encode(msg), (sys.argv[2], 161)) # Set host

substrate, _ = sock.recvfrom(2048)

# noinspection PyRedeclaration
rMsg, _ = decoder.decode(substrate, asn1Spec=msg)

print('received: %s' % rMsg.prettyPrint())

But i dont got any response from device. Snmp-get request work fine. Of course, before writing message i am was played a lot with net snmp and perform snmp walk/set/get requests, all works fine. Can someone help ? Thanks

1

There are 1 best solutions below

1
On

Hah, it was my error, sorry. I am send snmp-set to host that does not support this oid, so it should to be other. As result, i fixed problem by swaping host address for device that support requested oid. Thanks.