我正在尝试使用以下代码执行简单的 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 walk/set/get 请求,一切正常。有人可以帮忙吗?谢谢