首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过PyASN1模块执行SNMP_SET

无法通过PyASN1模块执行SNMP_SET
EN

Stack Overflow用户
提问于 2021-03-30 00:17:48
回答 1查看 40关注 0票数 0

我正在尝试使用以下代码执行简单的snmp-set请求:

代码语言:javascript
复制
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遍历/设置/获取请求,一切都很好。有人能帮帮忙吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-03-30 16:39:48

哈,是我的错,对不起。我正在向不支持此oid主机发送snmp-set,因此它应该是other。因此,我通过交换支持所请求oid的设备的主机地址来修复问题。谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66857796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档