etingof / snmpsim

SNMP Simulator
http://snmplabs.com/snmpsim/
BSD 2-Clause "Simplified" License
376 stars 119 forks source link

Cannot retrieve any data from simulator using any version #176

Closed smietanka closed 3 months ago

smietanka commented 3 months ago

Hello, I am looked for any SNMP simulator that can be run on docker side and I found this project, but I can't get any data from device. I tried run snmpsim locally without docker and inside docker. I have prepared data named self.snmprec inside data folder and run it using command: snmpsim-command-responder --agent-udpv4-endpoint=127.0.0.1:161 --data-dir=/usr/local/snmpsim/data --process-user=snmpsim --process-group=snmpsim

of course I get logs and information that simulator is listening. After that I am trying easy snmpwalk snmpget -v3 -c public -l authPriv -u simulator -A auctoritas -X privatus 127.0.0.1 1.3 and get No SNMP response received before timeout

I am not sure what I am doing wrong,

lextm commented 3 months ago

No, you cannot use that binding at 127.0.0.1 as it prevents the daemon from taking any requests from outside the container.

This is basic container networking knowledge and has nothing to do with snmpsim. Any other networking app inside a container follows the same rule.

Please use things like 0.0.0.0 instead.

smietanka commented 3 months ago

Thanks for answerr. As I mentioned I was also run Simulator without docker, just using python and commands provided im quick start section in documentantion. The same behavior. Even I tried to reach demo.pysnmp.com with provided credentials. The same error. Anything else I am doing wrong?

lextm commented 3 months ago
  1. Which documentation are you referring to? Our team is working on the latest release and snmpsim-lextudio package, but even that remains a work in progress. You will have to wait till we move further, or dig further into the code base yourself.

    All other documentation site you visited (created by others) can be out-of-date.

  2. demo.pysnmp.com supports several test credentials, as we documented in this page. simulator isn't one of them.

smietanka commented 3 months ago
  1. I am using this page quick start so I believe most updated one for now.
  2. So when I am run command snmpwalk -v2c -c index demo.pysnmp.com 1.3.6 taken from one of example I thought it will returns me the same things as documented but now I am getting Error: No SNMP response received before timeout.

My scenario: So after running snmpsim-command-responder and trying to send something to this I was getting Error: No SNMP response received before timeout. but I am not sure if this is the real problem.. as I run docker again now with 0.0.0.0 binding,

This is my Dockerfile

FROM python:3.12-slim

RUN pip install pipenv

RUN pipenv --python 3.12

RUN pipenv install snmpsim-lextudio
RUN adduser --system snmpsim
RUN addgroup --system snmpsim

ADD data /usr/local/snmpsim/data
RUN chown -R snmpsim:snmpsim /usr/local/snmpsim/data

EXPOSE 161/udp

CMD pipenv run snmpsim-command-responder --agent-udpv4-endpoint=0.0.0.0:161 --data-dir=/usr/local/snmpsim/data --process-user=snmpsim --process-group=snmpsim $EXTRA_FLAGS

After running it it seems to work, and when I am trying to get something by using this command: snmpwalk -v3 -c public -l authPriv -u simulator -A auctoritas -X privatus 127.0.0.1 1.3

Simulator is crashing

Listening at UDP/IPv4 endpoint 0.0.0.0:161, transport ID 1.3.6.1.6.1.1.0
Shutting down variation modules:
Variation module "writecache" shutdown OK
Variation module "subprocess" shutdown OK
Variation module "error" shutdown OK
Variation module "multiplex" shutdown OK
Variation module "redis" shutdown OK
Variation module "delay" shutdown OK
Variation module "sql" shutdown OK
Variation module "notification" shutdown OK
Variation module "numeric" shutdown OK
Process terminated
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 45, in runDispatcher
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 207, in loop
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 188, in poll2
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 123, in readwrite
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 108, in readwrite
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 420, in handle_read_event
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 170, in handle_read
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/base.py", line 80, in _cbFun
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/entity/engine.py", line 151, in __receiveMessageCbFun
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/rfc3412.py", line 327, in receiveMessage
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/mpmod/rfc3412.py", line 509, in prepareDataElements
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/secmod/rfc3414/service.py", line 1048, in processIncomingMsg
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/secmod/rfc3414/priv/des.py", line 160, in decryptData
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/des.py", line 76, in decrypt
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/__init__.py", line 141, in generic_decrypt
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/__init__.py", line 96, in _cryptography_decrypt
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/des.py", line 45, in _cryptography_cipher
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/cryptography/hazmat/backends/__init__.py", line 11, in default_backend
ModuleNotFoundError: No module named 'cryptography.hazmat.backends.openssl'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/-x-v5uFv0/bin/snmpsim-command-responder", line 8, in <module>
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/snmpsim/commands/responder.py", line 916, in main
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 50, in runDispatcher
pysnmp.error.PySnmpError: poll error: Traceback (most recent call last):
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 45, in runDispatcher
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 207, in loop
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 188, in poll2
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 123, in readwrite
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 108, in readwrite
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/asyncore/__init__.py", line 420, in handle_read_event
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 170, in handle_read
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/carrier/base.py", line 80, in _cbFun
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/entity/engine.py", line 151, in __receiveMessageCbFun
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/rfc3412.py", line 327, in receiveMessage
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/mpmod/rfc3412.py", line 509, in prepareDataElements
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/secmod/rfc3414/service.py", line 1048, in processIncomingMsg
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmp/proto/secmod/rfc3414/priv/des.py", line 160, in decryptData
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/des.py", line 76, in decrypt
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/__init__.py", line 141, in generic_decrypt
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/__init__.py", line 96, in _cryptography_decrypt
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/pysnmpcrypto/des.py", line 45, in _cryptography_cipher
;  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.12/site-packages/cryptography/hazmat/backends/__init__.py", line 11, in default_backend
;ModuleNotFoundError: No module named 'cryptography.hazmat.backends.openssl'
caused by <class 'ModuleNotFoundError'>: No module named 'cryptography.hazmat.backends.openssl'
smietanka commented 3 months ago

Ok I have digging a little more, and tried several scenarios so I have found strange issue. I write helm chart for me to host this snmp simulator on k8s with service that exposing a node port using UDP. After this finally it started work (probably there was some issues with normal docker earlier).

Because I was used SNMPv3 and when I send a request by snmpwalk like this: snmpget -v3 -l authPriv -u simulator -A auctoritas -X privatus clusterIP:31161 1.3.6 then this from above exception is throw. Whenever I am calling the same simulator but for a different SNMP version then everything is working. I tried several times to reinstall this cryptography package but nothing help in this case. Could you please check if SNMPv3 is working on your side for Simulator hosted on docker?

Thanks for help

lextm commented 3 months ago

In short, my team is still working on an up-to-date revision to address documentation gaps. You will have to wait.

But we do finish some very simple guide on this page,

https://docs.lextudio.com/snmpsim-data/#local-simulation

Let us know if that teaches you enough on what you should expect from the simulator.

smietanka commented 3 months ago

Well, I understand the situation. Inside this guide there is provided an command to an example. snmpget -v2c -c network/wifi/ubiquiti-m5 demo.pysnmp.com sysDescr.0 I assumed this demo.pysnmp.com is properly working. But its fine,

So easily I would expect from this simulator to have an snmp v3 support. I also supposed that I have good input data but still I am getting when I am calling: snmpwalk -v3 -c public -l authPriv -u simulator -A auctoritas -X privatus 127.0.0.1 1.3.6 \/

Process terminated
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/carrier/asyncore/dispatch.py", line 45, in runDispatcher
  File "/usr/local/lib/python3.11/asyncore.py", line 212, in loop
    poll_fun(timeout, map)
  File "/usr/local/lib/python3.11/asyncore.py", line 193, in poll2
    readwrite(obj, flags)
  File "/usr/local/lib/python3.11/asyncore.py", line 128, in readwrite
    obj.handle_error()
  File "/usr/local/lib/python3.11/asyncore.py", line 113, in readwrite
    obj.handle_read_event()
  File "/usr/local/lib/python3.11/asyncore.py", line 425, in handle_read_event
    self.handle_read()
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/carrier/asyncore/dgram/base.py", line 170, in handle_read
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/carrier/base.py", line 80, in _cbFun
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/entity/engine.py", line 151, in __receiveMessageCbFun
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/proto/rfc3412.py", line 327, in receiveMessage
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/proto/mpmod/rfc3412.py", line 509, in prepareDataElements
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/proto/secmod/rfc3414/service.py", line 1048, in processIncomingMsg
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmp/proto/secmod/rfc3414/priv/des.py", line 160, in decryptData
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmpcrypto/des.py", line 76, in decrypt
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmpcrypto/__init__.py", line 141, in generic_decrypt
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmpcrypto/__init__.py", line 96, in _cryptography_decrypt
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/pysnmpcrypto/des.py", line 45, in _cryptography_cipher
  File "/root/.local/share/virtualenvs/-x-v5uFv0/lib/python3.11/site-packages/cryptography/hazmat/backends/__init__.py", line 11, in default_backend
ModuleNotFoundError: No module named 'cryptography.hazmat.backends.openssl'

For v1 and v2c everything is alright.

lextm commented 3 months ago

We didn't fully upgrade the simulator to latest PySNMP yet, so anything can happen (including crashes).

That is on our radar too. We expect to give an update some time next week.

lextm commented 3 months ago

While we are still working on a complete update, the cause of your failed attempt with SNMP v3 is actually clear now.

You can see from the article actually -n switch is needed to specify the context name. Without that, the simulator simply drops your request. The value of context name for a specific emulated device is set to the v1/v2c community name (for whatever reason), which is also the data file name.

So in your case, you need to read the simulator output to learn the right community name and then pass via -n.

smietanka commented 3 months ago

Unfortunetly I used -n switch and the same error. In my data I have a lot of data from setup-snmpsim-data.

PS X:\docker-snmpsim-master> snmpget -v2c -c network/wifi/ubiquiti-m5 127.0.0.1 sysDescr.0
SNMPv2-MIB::sysDescr.0 = DisplayString: Linux 2.6.32.61 #1 Wed May 21 18:37:41 EEST 2014 mips
Shutting down...
PS X:\docker-snmpsim-master> snmpget -v3 -l authPriv -u simulator -A auctoritas -n network/wifi/ubiquiti-m5 -X privatus 127.0.0.1 sysDescr.0
No SNMP response received before timeout

And same exception on simulator side with this No module named 'cryptography.hazmat.backends.openssl. Also I started considering how should I name files .snmprec to not be forced to add this -n and -c switches for tools. My application which must have a feature to communicate over SNMP to retrieve information from real devices is not have an option to pass something like context names or others. I would select only device ip address, SNMP version, auth key and private key eventually to get the data from this. I assumed that this simulator could help me. But btw did you checked on your side this -n? I am not sure if I am doing something wrong or what?

lextm commented 3 months ago

There seems to be something wrong with your machine, but beyond the scope of this repo or even our own fork.

BTW, we started to add unit test cases for PySNMP/PySMI and now the simulator, so you can refer to cases like this where we specified the context name (public) to match the data file name (public.snmprec). And you can see the test results on all major platforms.

If you don't like certain part of the simulator, such as the extra context name, you will have to fork and maintain your own copy.

smietanka commented 3 months ago

There seems to be something wrong with your machine

So... I have still digged a little bit more. I suppose the problem was with my dockerfile.. For me it was strange as everything was worked when I hosted simulator locally (on my host machine directly) but when I run in the same way on docker side, then this snmpv3 was crashed with this information that no module found.

I seen that you also provide a docker file for this and I tried it and it looks like it was started working.

So I don't know exactly what was the issue, but when I deleted pipenv from my dockerfile, and process-group switch, then SNMP v3 started work correctly.