nuurrss
nuurrss

Reputation: 25

Is there any way to get some information with snmp without oids?

Im started to deal with snmp. I want to get some information from devices in my network like an ip address, mac address, name and etc. I want to do network discovery thing. I have the code belwow. I dont want to use oid numbers for that informations. Is there any way to get the information without oids? Also, the output is my code is wrong. characters not showing properly

Code:

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()

Output::

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

Upvotes: 1

Views: 876

Answers (1)

Lex Li
Lex Li

Reputation: 63264

The entire SNMP stack is built upon OIDs, so it is impossible to go without them.

The output does not meet your expectation, because the actual data stored for certain OIDs (like the last two in your case) are encoded and not convertible to str. So the actual solution is to learn how they were encoded and decode the data properly.

That's not an easy task for SNMP beginners but good books can teach what TEXT-CONVENTION is and how it defines the encoding of complex object types (such as MacAddress https://github.com/lextudio/sharpsnmppro-mib/blob/master/SNMPv2-TC.txt#L92).

Upvotes: 0

Related Questions