bbird81 / Netbox-ipscanner

ip scan script for populating IPAM module in Netbox
MIT License
24 stars 9 forks source link

Module not found error #9

Closed lasag-2511 closed 1 year ago

lasag-2511 commented 1 year ago

Can anyone help me with this ? from extras.scripts import Script ModuleNotFoundError: No module named 'extras.scripts'

bbird81 commented 1 year ago

Hi lasag-2511, are you running this from the netbox machine, following the intructions from the Readme? The Script object from the library extras.script is obviously available only on the netbox machine.

lasag-2511 commented 1 year ago

Yes I followed same steps from read me.. I placed the scripts under /opt/netbox/netbox/scripts on netbox server

bbird81 commented 1 year ago

I don't think you correctly installed netbox on the machine 'cause if you had, extras library would be there.

Anyway, installing the extras library is a simple:

pip install extras or sudo pip install extras

Depending on your installation of netbox.

lasag-2511 commented 1 year ago

Thank you very much for your response. I see extras folder already under /opt/netbox/netbox/ /opt/netbox/netbox# ls extras/ admin.py choices.py context_managers.py filtersets.py graphql management plugins reports.py signals.py tests validators.py webhooks_worker.py api conditions.py dashboard filters.py init.py migrations pycache scripts.py tables urls.py views.py apps.py constants.py fields.py forms lookups.py models querysets.py search.py templatetags utils.py webhooks.py

I also tried to install manually using "pip install extras" but i still have the same issue My netbox version is 3.5.7 , python version - 3.8.10 /usr/lib/python3/dist-packages/requests/init.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported " Traceback (most recent call last): File "netbox-scanner.py", line 2, in from extras.scripts import Script ModuleNotFoundError: No module named 'extras.scripts'

For me it looks like extras.scripts is broken with new netbox versions or python version ? kindly assist

candlerb commented 1 year ago

For me it looks like extras.scripts is broken with new netbox versions or python version ?

No, your installation is broken. For comparison, here's what I get with nbshell:

/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py nbshell
### NetBox interactive shell (netbox3)
### Python 3.8.10 | Django 4.1.10 | NetBox 3.5.9
### lsmodels() will show available models. Use help(<model>) for more info.
>>> from extras.scripts import Script
>>>

No problem there.

/usr/lib/python3/dist-packages/requests/init.py:89: RequestsDependencyWarning

The path shows you are not running Netbox inside its virtualenv (that is you are using global python packages which your system package manager provides), and it will be completely broken if you do that. All the libraries it uses should be in /opt/netbox/venv/... and it should not touch any system packages.

Please follow the installation instructions. You should be running netbox gunicorn under systemd as it described here. The supplied systemd unit file uses the correct path to initialize Netbox inside its virtualenv.

lasag-2511 commented 1 year ago

Hi @candlerb : Thank you very much for the faster response.
My bad Its my mistake provided wrong path. i have packages installed in the virtual environment of netbox as per the readme document. steps i followed: correct me if i am wrong

cd /opt/netbox
source venv/bin/activate
followed readme  and installed accordingly 

No, your installation is broken. For comparison, here's what I get with nbshell - I have the same output like yours

/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py nbshell
### NetBox interactive shell (netbox.com)
### Python 3.8.10 | Django 4.1.10 | NetBox 3.5.7
### lsmodels() will show available models. Use help(<model>) for more info.
>>> from extras.scripts import Script
>>>

This is my netbox service, like the same link you have provided me. Is nt this right?

     Loaded: loaded (/etc/systemd/system/netbox.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-08-30 11:30:33 UTC; 1h 34min ago
       Docs: https://docs.netbox.dev/
   Main PID: 33582 (gunicorn)
      Tasks: 11 (limit: 4595)
     Memory: 706.4M
     CGroup: /system.slice/netbox.service
             ├─33582 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─33584 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─33585 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─33586 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             ├─33587 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi
             └─33588 /opt/netbox/venv/bin/python3 /opt/netbox/venv/bin/gunicorn --pid /var/tmp/netbox.pid --pythonpath /opt/netbox/netbox --config /opt/netbox/gunicorn.py netbox.wsgi 

executed script inside venv:

(venv) user@netbox:/opt/netbox/netbox/scripts# python3 netbox-scanner.py
Traceback (most recent call last):
  File "netbox-scanner.py", line 2, in <module>
    from extras.scripts import Script
ModuleNotFoundError: No module named 'extras.scripts'
candlerb commented 1 year ago

(venv) user@netbox:/opt/netbox/netbox/scripts# python3 netbox-scanner.py

Did anybody say you could run code which uses Netbox libraries in that way? If so, where?

The netbox-scanner.py in this git repo is a Netbox Custom Script. You have to install it in the correct place, and run it from within the web GUI. Because it's running inside Netbox, the environment will be set up correctly for it.

In older versions of Netbox, you would copy it to /opt/netbox/netbox/scripts/ directory by hand. In more recent versions (I believe v3.5.0+), you have to upload it via the web interface: see Customization > Scripts in the navigation bar.

This is also described under the Usage heading in this repo.

lasag-2511 commented 1 year ago

(venv) user@netbox:/opt/netbox/netbox/scripts# python3 netbox-scanner.py

Did anybody say you could run code which uses Netbox libraries in that way? If so, where?

The netbox-scanner.py in this git repo is a Netbox Custom Script. You have to install it in the correct place, and run it from within the web GUI. Because it's running inside Netbox, the environment will be set up correctly for it.

In older versions of Netbox, you would copy it to /opt/netbox/netbox/scripts/ directory by hand. In more recent versions (I believe v3.5.0+), you have to upload it via the web interface: see Customization > Scripts in the navigation bar.

This is also described under the Usage heading in this repo.

Thank you very much. it did work when i have added with out tag based scanning. When I add the script with tag based scanning then i have the below error.

Script file at: /opt/netbox/netbox/scripts/netboxscanner.py could not be loaded.