catching exceptions with python easysnmp

1.8k Views Asked by At

I cannot seem to find documentation or examples of how to catch exceptions with Python's easySNMP library

https://easysnmp.readthedocs.io/en/latest/exceptions.html shows what exceptions are rasied but I get errors when trying to catch them

Simplified code:

from easysnmp import Session
try:
    session = Session(hostname=host,community=community, version=2)
except:
    print("ERROR - SNMP error:")
    sys.exit(3)    

def check_snmp(oid):
    try:
        ret = session.get(oid).value
    except easysnmp.EasySNMPNoSuchInstanceError:
        ## Return false if OID doesn't exists
        return 0
    except session.EasySNMPError as e:
        ## Print the error on general errors
        print("ERROR - SNMP error: {}".format(e))
    return 1

if check_snnp('ifalias.4'):
    print("SNMP returned true")

Output:

Traceback (most recent call last):
    File "./check_ip_route", line 72, in <module>
    if check_snmp(oid):
    File "./check_snmp", line 45, in check_snmp
        except easysnmp.EasySNMPNoSuchInstanceError:
    NameError: name 'easysnmp' is not defined
1

There are 1 best solutions below

5
Kishor Pawar On BEST ANSWER

Your error says name 'easysnmp' is not defined.

That is because you haven't imported it.

Instead you imported from easysnmp import Session

you need to do import easysnmp