我正在尝试使用以下代码执行简单的snmp-set请求:
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-get请求工作正常。当然,在写消息之前,我经常使用net snmp并执行snmp遍历/设置/获取请求,一切都很好。有人能帮帮忙吗?谢谢
发布于 2021-03-30 16:39:48
哈,是我的错,对不起。我正在向不支持此oid主机发送snmp-set,因此它应该是other。因此,我通过交换支持所请求oid的设备的主机地址来修复问题。谢谢。
https://stackoverflow.com/questions/66857796
复制相似问题