Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pynetbox.lib.query.RequestError: The request failed with code 400 Bad Request #4

Closed
omarmarquez opened this issue Apr 4, 2019 · 5 comments
Assignees
Labels
invalid This doesn't seem right

Comments

@omarmarquez
Copy link

I get this error both in Centos 7.6 and in a container running debian and python 3.7.3

My netbox is versionn 2.5.8

root@b456e5f91474:~/netbox-scanner# pip freeze

asn1crypto==0.24.0
bcrypt==3.1.4
certifi==2018.8.24
cffi==1.11.5
chardet==3.0.4
cpe==1.2.1
cryptography==2.3.1
idna==2.7
netaddr==0.7.19
paramiko==2.4.2
pyasn1==0.4.4
pycparser==2.19
PyNaCl==1.3.0
pynetbox==3.4.6
python-nmap==0.6.1
requests==2.19.1
six==1.11.0
urllib3==1.23

root@b456e5f91474:~/netbox-scanner# python --version
Python 3.7.3

Here is the stack trace:

root@b456e5f91474:~/netbox-scanner# python netbox-scanner-master/netbox-scanner/nbscanner
Traceback (most recent call last):
  File "netbox-scanner-master/netbox-scanner/nbscanner", line 75, in <module>
    nbs.sync(networks)
  File "/root/netbox-scanner/netbox-scanner-master/netbox-scanner/nbscanner.py", line 223, in sync
    self.sync_network(network)
  File "/root/netbox-scanner/netbox-scanner-master/netbox-scanner/nbscanner.py", line 157, in sync_network
    self.sync_host(host)       
  File "/root/netbox-scanner/netbox-scanner-master/netbox-scanner/nbscanner.py", line 131, in sync_host
    nbhost = self.netbox.ipam.ip_addresses.get(address=host[0])
  File "/usr/local/lib/python3.7/site-packages/pynetbox/lib/endpoint.py", line 157, in get
    filter_lookup = self.filter(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/pynetbox/lib/endpoint.py", line 247, in filter
    for i in req.get()
  File "/usr/local/lib/python3.7/site-packages/pynetbox/lib/query.py", line 240, in get
    return req_all(self.url)
  File "/usr/local/lib/python3.7/site-packages/pynetbox/lib/query.py", line 222, in req_all
    req = make_request(url)
  File "/usr/local/lib/python3.7/site-packages/pynetbox/lib/query.py", line 219, in make_request
    raise RequestError(req)
pynetbox.lib.query.RequestError: The request failed with code 400 Bad Request

@lopes
Copy link
Owner

lopes commented Apr 4, 2019

It looks like a mismatch between pynetbox v3.4.6 and NetBox v2.5.8 --I built this script using NetBox v2.4.4 and current version of pynetbox is 4.0.6.

I can't reproduce this environment right now (NetBox v2.5.8), but just made a new commit updating all versions of dependencies. Please, try this new version and send me a reply (it worked for me pynetbox v4.0.6 and NetBox v2.4.4).

@omarmarquez
Copy link
Author

Ok,
downloaded new zip from master:

pip install --upgrade -r requirements.txt

root@b456e5f91474:~/netbox-scanner# pip freeze
asn1crypto==0.24.0
bcrypt==3.1.6
certifi==2019.3.9
cffi==1.12.2
chardet==3.0.4
cpe==1.2.1
cryptography==2.6.1
idna==2.8
netaddr==0.7.19
paramiko==2.4.2
pyasn1==0.4.5
pycparser==2.19
PyNaCl==1.3.0
pynetbox==4.0.6
python-nmap==0.6.1
requests==2.21.0
six==1.12.0
urllib3==1.24.1

i see pynetbox changing to 4.0.6 ...
Running the scanner, i get the same error:

root@b456e5f91474:~/netbox-scanner# python netbox-scanner-master/netbox-scanner/nbscanner
Traceback (most recent call last):
  File "netbox-scanner-master/netbox-scanner/nbscanner", line 75, in <module>
    nbs.sync(networks)
  File "/root/netbox-scanner/netbox-scanner-master/netbox-scanner/nbscanner.py", line 223, in sync
    self.sync_network(network)
  File "/root/netbox-scanner/netbox-scanner-master/netbox-scanner/nbscanner.py", line 157, in sync_network
    self.sync_host(host)       
  File "/root/netbox-scanner/netbox-scanner-master/netbox-scanner/nbscanner.py", line 131, in sync_host
    nbhost = self.netbox.ipam.ip_addresses.get(address=host[0])
  File "/usr/local/lib/python3.7/site-packages/pynetbox/core/endpoint.py", line 135, in get
    filter_lookup = self.filter(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/pynetbox/core/endpoint.py", line 220, in filter
    ret = [self._response_loader(i) for i in req.get()]
  File "/usr/local/lib/python3.7/site-packages/pynetbox/core/query.py", line 239, in get
    return req_all(self.url)
  File "/usr/local/lib/python3.7/site-packages/pynetbox/core/query.py", line 217, in req_all
    req = make_request(url)
  File "/usr/local/lib/python3.7/site-packages/pynetbox/core/query.py", line 214, in make_request
    raise RequestError(req)
pynetbox.core.query.RequestError: The request failed with code 400 Bad Request but more specific details were not returned in json. Check the NetBox Logs or investigate this exception's error attribute.

In the server side running in development mode with DEbug=True I see:

----------------------------------------
Exception happened during processing of request from ('10.0.4.6', 56748)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/local/lib/python3.7/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python3.7/socketserver.py", line 720, in __init__
    self.handle()
  File "/usr/local/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 171, in handle
    self.handle_one_request()
  File "/usr/local/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 179, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "/usr/local/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
----------------------------------------
----------------------------------------
Exception happened during processing of request from ('10.0.4.6', 56752)
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/local/lib/python3.7/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python3.7/socketserver.py", line 720, in __init__
    self.handle()
  File "/usr/local/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 171, in handle
    self.handle_one_request()
  File "/usr/local/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 179, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "/usr/local/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
----------------------------------------


@lopes
Copy link
Owner

lopes commented Apr 4, 2019

pynetbox.core.query.RequestError: The request failed with code 400 Bad Request but more specific details were not returned in json. Check the NetBox Logs or investigate this exception's error attribute.

Connection reset errors could be related to network or web server issues. Using the same environment you're running the script, you could open Python shell and simulate that interaction:

>>> from pynetbox import api
>>> nb = api('netbox_addr', token='token_goes_here', ssl_verify=False)
>>> nb.ipam.ip_addresses.get(address='ip_addr')

Try with IP addresses that are registered and not registered in NetBox. You could even try with some invalid values, like address=None or address=84. How will NetBox behave?

@lopes
Copy link
Owner

lopes commented Apr 4, 2019

@omarmarquez
Copy link
Author

Thanks, this actually was helpful debugging.
The issue seems to have been caused by using the short name for the netbox server in the scanner config instead of the fqdn that Netbox has in the ALLOW_HOSTS setting.
With this and pynetbox==4.0.6 now the scanner is aadding new IPs to netbox!!!

@lopes lopes added the invalid This doesn't seem right label May 26, 2020
@lopes lopes self-assigned this May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants