【问题标题】:Is there any way to get some information with snmp without oids?有没有办法用没有oid的snmp获取一些信息?
【发布时间】:2021-12-10 09:22:59
【问题描述】:

我开始处理snmp。我想从我的网络中的设备获取一些信息,例如 ip 地址、mac 地址、名称等。我想做网络发现的事情。 我有下面的代码。我不想为这些信息使用 oid 数字。有没有办法在没有oid的情况下获取信息? 另外,输出是我的代码错误。字符显示不正确

代码:

from pysnmp.hlapi import *
import datetime

class SNMP_QUERY():
    def __init__(self):
        pass

    def __del__(self):
        pass


def snmp_query(host, community, oid):
    errorIndication, errorStatus, errorIndex, varBinds = next(
        getCmd(SnmpEngine(),
               CommunityData(community),
               UdpTransportTarget((host, 161)),
               ContextData(),
               ObjectType(ObjectIdentity(oid)),
               lookupMib=False))

    if errorIndication:
        print(errorIndication)
    else:
        if errorStatus:
            print('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex) - 1] or '?'
            )
                  )
        else:
            for name, val in varBinds:
                return (str(val))


def main():
    # Network Device
    host = '127.0.0.1'
    community = 'public'

    #overviewModelName = '.1.3.6.1.2.1.1.1.0'
    name = '1.3.6.1.2.1.1.5.0'
    macAddres = '.1.3.6.1.2.1.2.2.1.6.12'
    io_address = '.1.3.6.1.2.1.4.21.1.1.192.168.1.27'

    result = {}
   # result['Model Name'] = snmp_query(host, community, overviewModelName)
    result['Name'] = snmp_query(host, community, name)
    result['Mac Adress'] = snmp_query(host, community, macAddres)
    result['IP'] = snmp_query(host, community, io_address)

    print(result)

if __name__ == '__main__':
    main()

输出::

{'Name': 'DESKTOP-ANIHIT4', 'Mac Adress': 'ÜA©Hð&', 'IP': 'À¨\x01\x1b'}

【问题讨论】:

  • 您的输出似乎正确。 IP 和 MAC 地址不是字符串,而是一系列字节。检查this answer,了解如何将它们转换为人类可读的文本。

标签: python snmp pysnmp snmp-trap


【解决方案1】:

整个 SNMP 堆栈都建立在 OID 之上,因此没有它们是不可能的。

输出不符合您的期望,因为为某些 OID(如您的情况下的最后两个)存储的实际数据已编码并且无法转换为 str。所以实际的解决方案是了解它们是如何正确编码和解码数据的。

这对于 SNMP 初学者来说不是一件容易的事,但好书可以教 TEXT-CONVENTION 是什么以及它如何定义复杂对象类型的编码(例如 MacAddresshttps://github.com/lextudio/sharpsnmppro-mib/blob/master/SNMPv2-TC.txt#L92)。

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多