无法通过PyASN1模块执行SNMP_SET



我正在尝试用以下代码执行简单的snmp设置请求:

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())

但我并没有从设备上得到任何回应。Snmp获取请求工作正常。当然,在编写消息之前,我经常使用net-snmp并执行snmpwalk/set/get请求,一切都很好。有人能帮忙吗?感谢

哈,这是我的错误,对不起。我将snmp集发送到不支持此oid的主机,所以它应该是其他的。因此,我通过交换支持请求oid的设备的主机地址来解决问题。谢谢

最新更新