ktbyers / pynet

Python for Network Engineers
Apache License 2.0
582 stars 516 forks source link

SNMPv3 on linux(Ubuntu 16.04) with Python3 - script "snmp_helper.py" #12

Open mathewfer opened 6 years ago

mathewfer commented 6 years ago

Hi Kirk,

I tried to run this script "snmp_helper.py" in https://github.com/ktbyers/pynet/tree/master/snmp But I import error. Your script says "Requires the pysnmp4 library" but I see it is already there.

Do you have any idea what is missing?

mathew@Tools-SRV:~$ python2
Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from snmp_helper import snmp_get_oid_v3,snmp_extract
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named snmp_helper
>>> quit()
mathew@Tools-SRV:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from snmp_helper import snmp_get_oid,snmp_extract
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'snmp_helper'
>>> 

root@Tools-SRV:/home/mathew# pip3 install pysnmp
Requirement already satisfied: pysnmp in /usr/local/lib/python3.5/dist-packages (4.3.9)
Requirement already satisfied: pycryptodome in /usr/local/lib/python3.5/dist-packages (from pysnmp) (3.4.7)
Requirement already satisfied: pysmi in /usr/local/lib/python3.5/dist-packages (from pysnmp) (0.0.7)
Requirement already satisfied: pyasn1>=0.2.3 in /usr/local/lib/python3.5/dist-packages (from pysnmp) (0.4.2)
Requirement already satisfied: ply in /usr/local/lib/python3.5/dist-packages (from pysmi->pysnmp) (3.9)
root@Tools-SRV:/home/mathew# pip3 install snmp_helper
Collecting snmp_helper
  Could not find a version that satisfies the requirement snmp_helper (from versions: )
No matching distribution found for snmp_helper
root@Tools-SRV:/home/mathew#
ktbyers commented 6 years ago

@mathewfer Did you download and install 'snmp_helper.py' such that it is somewhere on your Python Path (the easiest place being your current working directory)?

Kirk

mathewfer commented 6 years ago

Hi Kirk, Oops, my bad. Thanks a lot and sorry for the trouble and yes got it working as expected.

mathewfer commented 6 years ago

Hi Kirk,

I progressed but see below and please let me know if you have any idea.

When I run "snmpwalk -v3 -On -u aYYuv3 -l AuthPriv -a SHA -A undxyz1 -X undxyz3 10.1.1.23 1.3.6.1.2.1.1.1.0", router responds well.

mathewfer@Tools-SRV:~$ cat snmpv3.py
#Example usage (SNMPv3):
from snmp_helper import snmp_get_oid_v3,snmp_extract

#The below works
#snmpwalk -v3 -On -u aYYuv3 -l AuthPriv -a SHA -A undxyz1 -X undxyz3 10.1.1.23 1.3.6.1.2.1.1.1.0

snmp_device = ('10.1.1.23', 161)
a_user = 'aYYuv3'
auth_key = 'undxyz1'
encrypt_key = 'undxyz3'
encrypt_proto='des'
auth_proto = 'sha'
snmp_user = (a_user, auth_key, encrypt_key)
sys_descr = '1.3.6.1.2.1.1.1.0'
snmp_data = snmp_get_oid_v3(snmp_device, snmp_user, oid=sys_descr)
output = snmp_extract(snmp_data)
print(output)
#end

mathewfer@Tools-SRV:~$ python3 snmpv3.py 
Traceback (most recent call last):
  File "<string>", line 1148, in __delegate
  File "<string>", line 937, in writeTest
pysnmp.smi.error.RowCreationWanted: RowCreationWanted({})

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "snmpv3.py", line 17, in <module>
    snmp_data = snmp_get_oid_v3(snmp_device, snmp_user, oid=sys_descr)
  File "/home/mathewfer/snmp_helper.py", line 115, in snmp_get_oid_v3
    lookupNames=True, lookupValues=True
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/entity/rfc3413/oneliner/cmdgen.py", line 183, in getCmd
    **kwargs):
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 111, in getCmd
    lookupMib=options.get('lookupMib', True)))
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/hlapi/asyncore/cmdgen.py", line 124, in getCmd
    addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget)
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/hlapi/lcd.py", line 60, in configure
    securityName=authData.securityName
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/entity/config.py", line 142, in addV3User
    ((usmUserEntry.name + (13,) + tblIdx1, 'destroy'),)
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/smi/instrum.py", line 256, in writeVars
    return self.flipFlopFsm(self.fsmWriteVar, varBinds, acInfo)
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/smi/instrum.py", line 221, in flipFlopFsm
    rval = f(tuple(name), val, idx, acInfo)
  File "<string>", line 499, in writeTest
  File "<string>", line 1177, in writeTest
  File "<string>", line 1154, in __delegate
  File "<string>", line 1138, in __manageColumns
  File "<string>", line 837, in createTest
  File "<string>", line 737, in createTest
  File "<string>", line 624, in setValue
  File "<string>", line 48, in setValue
  File "/usr/local/lib/python3.5/dist-packages/pysnmp/proto/rfc1902.py", line 203, in clone
    return univ.OctetString.clone(self, *args, **kwargs).setFixedLength(self.getFixedLength())
  File "/usr/local/lib/python3.5/dist-packages/pyasn1/type/base.py", line 349, in clone
    return self.__class__(value, **initilaizers)
  File "/usr/local/lib/python3.5/dist-packages/pyasn1/type/univ.py", line 819, in __init__
    base.AbstractSimpleAsn1Item.__init__(self, value, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pyasn1/type/base.py", line 240, in __init__
    value = self.prettyIn(value)
  File "<string>", line 364, in prettyIn
  File "/usr/local/lib/python3.5/dist-packages/pyasn1/type/univ.py", line 878, in prettyIn
    return bytes(value)
TypeError: 'NoneType' object is not iterable
mathewfer@Tools-SRV:~$
ktbyers commented 6 years ago

@mathewfer One issue is the following (there might be others)...you need to specify the auth_proto and encrypt_proto in your call to snmp_get_oid_v3()

The below is the function definition.

def snmp_get_oid_v3(snmp_device, snmp_user, oid='.1.3.6.1.2.1.1.1.0', auth_proto='sha',
                    encrypt_proto='aes128', display_errors=True):

So with they way you are currently calling snmp_get_oid_v3() it is going to try to use aes128.

Kirk