简体   繁体   English

通过 Pysnmp 从某个端口发送 SNMP 消息

[英]Sending SNMP messages from certain port by Pysnmp

I am getting signal information from a device in my network by using python pysnmp.我正在使用 python pysnmp 从网络中的设备获取信号信息。

for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(
        SnmpEngine(),
        UsmUserData(userName=config.transmitter_username, authKey=config.transmitter_authkey),
        UdpTransportTarget((X.X.X.X, 161), timeout=1.0, retries=0),
        ContextData(),
        ObjectType(ObjectIdentity(SNMP_INITIAL_INFO_OIDS[0][1])),
        lexicographicMode=False
):
    if errorIndication or errorStatus:

There is no problem with the request, but I am doing this periodically (once in every 5 seconds), and everytime I send the request, target port is 161 but source port differs, which is expected since it finds whatever available port at that moment.请求没有问题,但我定期执行此操作(每 5 秒一次),并且每次发送请求时,目标端口为 161,但源端口不同,这是预期的,因为它会在那时找到任何可用端口. I also want to fix that port but do not know how to do it with pysnmp.hlapi.我也想修复该端口,但不知道如何使用 pysnmp.hlapi 进行修复。 What I need is binding the port that is used to send SNMP over pysnmp.我需要的是绑定用于通过 pysnmp 发送 SNMP 的端口。 I googled it but could not find much so I am here.我用谷歌搜索但找不到太多所以我在这里。

Try calling .setLocalAddress on the hlapi.UdpTransportTarget object.尝试在hlapi.UdpTransportTarget对象上调用.setLocalAddress That should make your end of the socket bound to this specific address.这应该使您的套接字结束绑定到此特定地址。

Perhaps it makes sense to keep that endpoint permanently allocated so that you won't bump into a busy port grabbed by some other process.也许保持该端点永久分配是有意义的,这样您就不会遇到其他进程占用的繁忙端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM