jagter / python-netbox Goto Github PK
View Code? Open in Web Editor NEWPython Netbox Client
License: Apache License 2.0
Python Netbox Client
License: Apache License 2.0
netbox.ipam.get_next_available_ip(prefix='x.x.x.x/xx')
File "/usr/local/lib/python3.9/site-packages/netbox/ipam.py", line 178, in get_next_available_ip
return self.netbox_con.get(param, limit=1)[0]['address']
File "/usr/local/lib/python3.9/site-packages/netbox/connection.py", line 96, in get
return resp_data['results']
TypeError: list indices must be integers or slices, not str
hi,
thanks for the tool.
I am trying to use the tool to create an interface: and hitting this error:
netbox.dcim.create_interface(name='test', device_id='rt01', form_factor=[1000])
[...]
Traceback (most recent call last):
[...]
netbox.exceptions.CreateException: Creation failed with unknown reason
I did see netbox code and it looks like Jeremy has renamed form_factor to type, and form_factor will be removed from v2.7: https://github.com/netbox-community/netbox/pull/3067/files
however, even using those codes results in the above error. unsure if this is already being worked upon?
thanks
netbox.virtualization.get_virtual_machines() returns 50 items tops.
I've noticed that when we hit an API endpoint the corresponding URL is printed.
For example calling:
netbox.dcim.get_manufacturers()
will print this to stdout:
http://<nb_ip>:<np_port>/api/dcim/manufacturers/?limit=0
For large scripts this can cause a lot of unnecessary output. What are your thoughts on a PR that would would make the default behavior to not print the url unless netbox.dcim.get_manufacturers(url_debug=True)
Hi,
nb.dcim.inventory.all() does not work. According to netbox document, the url is "/api/dcim/inventory-items/"
I think the url generated by pynetbox is missing "-items"
netbox.ipam.create_ip_address(str(ipaddress))
File "ipscan.py", line 44, in
netbox.ipam.create_ip_address(str(ipaddress))
File "/home/ec2-admin/.local/lib/python3.8/site-packages/netbox/ipam.py", line 62, in create_ip_address
return self.netbox_con.post('/ipam/ip-addresses/', required_fields, **kwargs)
File "/home/ec2-admin/.local/lib/python3.8/site-packages/netbox/connection.py", line 129, in post
raise exceptions.CreateException(resp_data)
File "/home/ec2-admin/.local/lib/python3.8/site-packages/netbox/exceptions.py", line 36, in init
self.err = ''.join('{} '.format(val[0]) for key, val in resp_data.items())
File "/home/ec2-admin/.local/lib/python3.8/site-packages/netbox/exceptions.py", line 36, in
self.err = ''.join('{} '.format(val[0]) for key, val in resp_data.items())
TypeError: 'int' object is not subscriptable
$ pip install python-netbox
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: python-netbox in /home/ec2-admin/.local/lib/python3.8/site-packages (0.0.18)
Requirement already satisfied: ipaddress in /home/ec2-admin/.local/lib/python3.8/site-packages (from python-netbox) (1.0.23)
Requirement already satisfied: requests in /home/ec2-admin/.local/lib/python3.8/site-packages (from python-netbox) (2.23.0)
Requirement already satisfied: chardet<4,>=3.0.2 in /home/ec2-admin/.local/lib/python3.8/site-packages (from requests->python-netbox) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/ec2-admin/.local/lib/python3.8/site-packages (from requests->python-netbox) (1.25.9)
Requirement already satisfied: idna<3,>=2.5 in /home/ec2-admin/.local/lib/python3.8/site-packages (from requests->python-netbox) (2.9)
Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-admin/.local/lib/python3.8/site-packages (from requests->python-netbox) (2020.4.5.1)
Looks like the 0.0.20 release introduced a bug somewhere, after the update from .19 i get the following error:
from netbox import NetBox
n = NetBox(host='netbox.domain.tld', auth_token='whatever')
x = n.ipam.get_ip_addresses(parent='1.2.3.0/24')
Traceback (most recent call last):
File "/builds/run.py", line 5, in export_reverse
address_list = netbox.ipam.get_ip_addresses(parent='1.2.3.0/24')
File "/usr/local/lib/python3.9/site-packages/netbox/ipam.py", line 36, in get_ip_addresses
return self.netbox_con.get('/ipam/ip-addresses/', **kwargs)
File "/usr/local/lib/python3.9/site-packages/netbox/connection.py", line 85, in get
'&'.join('{}={}'.format(key, urllib.parse.quote(val)) for key, val in kwargs.items()), limit)
File "/usr/local/lib/python3.9/site-packages/netbox/connection.py", line 85, in <genexpr>
'&'.join('{}={}'.format(key, urllib.parse.quote(val)) for key, val in kwargs.items()), limit)
File "/usr/local/lib/python3.9/urllib/parse.py", line 862, in quote
return quote_from_bytes(string, safe)
File "/usr/local/lib/python3.9/urllib/parse.py", line 887, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
Hello,
It seems there are no get_device_by_name()
method anymore. But it is mentioned in the documentation:
https://python-netbox.readthedocs.io/en/latest/examples.html#examples
So did by misstake use our read token and it took me a long time to find that.
That got me thinking and i did a test with a bad token and its not displayed in any way so its super hard to find out.
So in connection.py i just added a line to print
print(response_data, response.status_code) return response.ok, response.status_code, response_data
'ipython3 1.py
'/home/chrnic/.local/lib/python3.8/site-packages/netbox/connection.py:39: SyntaxWarning: "is not" with a literal. Did you mean "!="?
if method is not 'GET':
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:999: InsecureRequestWarning: Unverified HTTPS request is being made to host 'netbox.FAKE.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
{'detail': 'Invalid token'} 403
I dont know where it would be proper to catch and handle bad token or "unauthorized"
I noticed that the library does nothing with the provided next field of the api, and therefor stays quiet about the fact that not all values were returned.
Can something be added (an extra key for example) which automatically request the next url (based on the next field) and add it to the to return list.
https://127.0.0.1/api/ipam/prefixes/
HTTP 200 OK
Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 1160,
"next": "https://127.0.0.1/api/ipam/prefixes/?limit=1000?offset=1000",
https://127.0.0.1/api/ipam/prefixes/?limit=1000?offset=1000
HTTP 200 OK
Allow: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"count": 1160,
"next": "https://127.0.0.1/api/ipam/prefixes/?limit=1000&offset=2000",
"previous": "https://127.0.0.1/api/ipam/prefixes/?limit=1000",
I now do this as temporary solution
prefixes = []
offset_num = 0
while 1:
templist = netbox.ipam.get_ip_prefixes(offset=offset_num)
if templist:
prefixes.extend(templist)
offset_num += 1000
templist = []
else:
break
The loop continues until an empty list is returned, which requires an extra unnecessary api call.
Netbox 2.10 introduced REST API support for managing custom fields:
netbox-community/netbox#5274
Should be able to add this to extras in python-netbox
Hello, I know the recent release of netbox allowed for the edit of the local config context via the API. Has this been rolled into the python api yet?
netbox.dcim.get_devices() returns 50 devices tops.
Can't get port to work correctly.
netbox = NetBox(
use_ssl=True,
host='*********',
auth_token='************',
port=8443
)
host_info = netbox.dcim.get_devices()
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=********', port=443)
Python netbox modules doesnot support for netbox later version i think. for ex: netbox.ipam.create_ip_address- doesnot work anymore
Netbox 2.10 introduced REST API support for bulk updates and bulk deletes:
netbox-community/netbox#3436
netbox-community/netbox#4882
Hi looking for a option to search ip prefixe based on ipaddress provided ... or is there something already there ?
eg:
netbox.ipam.get_ip_prefixes(address='192.168.34/32')
and returns me back relevant prefix for this as
prefix = 192.168.34.0/128
Please help
Hi,
I tried to get next available ip from prefix with function netbox.ipam.get_next_available_ip(id=my_id) but i got exception:
`TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20192/3300274695.py in
----> 1 netbox.ipam.get_next_available_ip(id=29)
c:\users\gs\appdata\local\programs\python\python37\lib\site-packages\netbox\ipam.py in get_next_available_ip(self, **kwargs)
174
175 param = '/ipam/prefixes/{}/available-ips/'.format(prefix_id)
--> 176 return self.netbox_con.get(param, limit=1)[0]['address']
177
178 def get_vrfs(self, **kwargs):
c:\users\gs\appdata\local\programs\python\python37\lib\site-packages\netbox\connection.py in get(self, param, key, limit, **kwargs)
94 resp_data = self.__request('GET', params=param, key=key, url=url)
95
---> 96 return resp_data['results']
97
98 def put(self, params):
TypeError: list indices must be integers or slices, not str`
Endpoint /api/ipam/prefixes/my_id/available-ips/ works fine so it seems to be a python-netbox issue, did you have already faced this issue?
Just investigating the usefulness of this library, and I found the ipam.get_next_available_ip
. Looked useful, but in its implementation, it calls get_ip_prefixes
and always uses the first prefix it finds as the basis. I cannot seem to provide it with filter kwargs that works, since it will always call the underlying netbox_con.get
method with `param='/ipam/prefixes/' and no way to change that. Any kwargs would just be appended as GET parameters, but if I interpret the netbox API correctly, I would need to change the param to ''/ipam/prefixes/'.
Am I overlooking something or it this a bug?
Hi
Currently, we have to use "name" as ID to update and delete an object.
Having a lot of objects with names including letters like "+" and "&", we're experiencing issues - like "Unable to find tenant"
Create the same objects with those names are not issues.
Sorry - but considering to search for another libary to overcome such issue...
Will ask for an option to use "ID" as ID for update/delete.
Hi, I find that is possible to .dcim.get_devices(role=role, tag=tag)
How can I query for multiple roles ?
With Netbox UI it's possible via https://URL/dcim/devices/?q=&role=role1&role=role2
Thx in advance :)
The whole circuits api subtree is missing.
Can you add it?
When running following code:
for role in netbox.ipam.get_roles():
netbox.ipam.delete_role(role_name=role['name'])
I get :
netbox.exceptions.DeleteException: Delete failed with an unknown reason
Netbox logs say
2020-07-10 12:47:28 +0000] [35] [DEBUG] DELETE /api/ipam/role/17
172.18.0.7 - - [10/Jul/2020:12:47:28 +0000] "DELETE /api/ipam/role/17 HTTP/1.0" 404 20391 "-" "python-requests/2.24.0"
When requested the same deletion via API explorer, the same delete is working:
[2020-07-10 12:59:14 +0000] [34] [DEBUG] DELETE /api/ipam/roles/17/
172.18.0.7 - - [10/Jul/2020:12:59:14 +0000] "DELETE /api/ipam/roles/17/ HTTP/1.0" 204 0 "http://192.168.56.101:8000/api/docs/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0"
And the reason is that the method in ipam.py
:
def delete_role(self, role_name):
"""Delete prefix/vlan role
:param role_name: prefix/vlan role to delete
:return: bool True if successful otherwise raise DeleteException
"""
try:
role_id = self.get_roles(name=role_name)[0]['id']
except IndexError:
raise exceptions.NotFoundException('prefix/vlan role: {}'.format(role_name)) from None
return self.netbox_con.delete('/ipam/role/', role_id)
should contain
delete('/ipam/roles/', role_id)
instead of
delete('/ipam/role/', role_id)
When patched, all is working fine :)
Can you please fix it in?
x = netbox.dcim.get_devices(name__ic="ro")
returns all of my hosts not just hosts containing ro
in their name. But my understanding based on https://netbox.readthedocs.io/en/stable/api/filtering/ is that that should work...
Affected functions:
If the device type contains a plus sign (in my case https://mikrotik.com/product/CCR1016-12S-1Splus), the call does not error out but fails to find the data in netbox, looking into it the following URL is generated:
GET /api/dcim/device-types/?model=CCR1016-12S-1S+&limit=0 HTTP/1.1\r\n
Where it should be generating this:
GET /api/dcim/device-types/?model=CCR1016-12S-1S%2B&limit=0
import logging
import urllib.parse
from netbox import NetBox
LOGGING_FORMAT = '%(asctime)s - %(name)s - %(thread)d - %(levelname)s - %(message)s'
logging.basicConfig(level = logging.DEBUG, format=LOGGING_FORMAT, filename = 'output.txt')
m = 'CCR1016-12S-1S+'
n = NetBox(host='172.16.0.22', port=8000, use_ssl=False, auth_token='<auth token>')
unquoted = n.dcim.get_device_types(model=m)
logging.debug("Unquoted: {0}".format(unquoted))
quoted = n.dcim.get_device_types(model=urllib.parse.quote(m))
logging.debug("Unquoted: {0}".format(quoted))
Output file contents:
2021-04-08 03:06:31,712 - urllib3.connectionpool - 139725271254848 - DEBUG - Starting new HTTP connection (1): 172.16.0.22:8000
2021-04-08 03:06:33,391 - urllib3.connectionpool - 139725271254848 - DEBUG - http://172.16.0.22:8000 "GET /api/dcim/device-types/?model=CCR1016-12S-1S+&limit=0 HTTP/1.1" 200 52
2021-04-08 03:06:33,395 - root - 139725271254848 - DEBUG - Unquoted: []
2021-04-08 03:06:33,398 - urllib3.connectionpool - 139725271254848 - DEBUG - Starting new HTTP connection (1): 172.16.0.22:8000
2021-04-08 03:06:33,532 - urllib3.connectionpool - 139725271254848 - DEBUG - http://172.16.0.22:8000 "GET /api/dcim/device-types/?model=CCR1016-12S-1S%2B&limit=0 HTTP/1.1" 200 576
2021-04-08 03:06:33,535 - root - 139725271254848 - DEBUG - Unquoted: [{'id': 803, 'url': 'http://172.16.0.22:8000/api/dcim/device-types/803/', 'manufacturer': {'id': 49, 'url': 'http://172.16.0.22:8000/api/dcim/manufacturers/49/', 'name': 'MikroTik', 'slug': 'mikrotik'}, 'model': 'CCR1016-12S-1S+', 'slug': 'ccr1016-12s-1splus', 'display_name': 'MikroTik CCR1016-12S-1S+', 'part_number': '', 'u_height': 1, 'is_full_depth': False, 'subdevice_role': None, 'front_image': None, 'rear_image': None, 'comments': '', 'tags': [], 'custom_fields': {}, 'created': '2021-04-07', 'last_updated': '2021-04-07T18:20:59.123550Z', 'device_count': 1}]
Workaround:
Manually quote the appropriate entries.
Notes:
I believe this may affect many more functions as the create also needs to be pre-quoted to handle the create for a device_type containing a plus sign as the data is in the POST body, my fixed code is as follows:
rez = self._netbox.dcim.create_device(
name = dev_facts['hostname'],
device_role = 'router',
site_name = location_code,
device_type = urllib.parse.quote(dev_type[0]['model']),
serial = dev_facts['serial_number'],
)
I am guessing the best place to handle this would be __request in netbox/connection.py but we at least need to handle if the plus was placed there to replace a space in the URL or if it was part of the data itself.
Hi!
I try to connect to Netbox, but it failed! I dont use token or any passwords, becouse i want to GET data only. My script contains this code:
netbox = NetBox(host='172.24.213.34', port=8000, use_ssl=False) devices = netbox.dcim.get_devices()
But:
Traceback (most recent call last): File "C:/Users/vartemenok/PycharmProjects/NETBOX_API_v0.1/main.py", line 6, in <module> devices = netbox.dcim.get_devices() File "C:\Users\vartemenok\PycharmProjects\NETBOX_API_v0.1\venv\lib\site-packages\netbox\dcim.py", line 95, in get_devices return self.netbox_con.get('/dcim/devices/', **kwargs) File "C:\Users\vartemenok\PycharmProjects\NETBOX_API_v0.1\venv\lib\site-packages\netbox\connection.py", line 87, in get resp_ok, resp_status, resp_data = self.__request('GET', params=param, key=key, url=url) File "C:\Users\vartemenok\PycharmProjects\NETBOX_API_v0.1\venv\lib\site-packages\netbox\connection.py", line 62, in __request raise ConnectionError(err_msg) ConnectionError: Unable to connect to Netbox host: 172.24.213.34
When i use POSTMAN there are no problems.
If integer kwargs are passed into NetboxConnection.get()
, urllib.parse.quote_from_bytes()
throws an exception because it doesn't know how to escape the values. This reproduces on python-netbox 0.0.20.
from netbox import NetBox
host = "host"
token = "token"
nb = NetBox(host=host, auth_token=token, ssl_verify=False)
offset = 0
devices = nb.dcim.get_devices(limit=0, offset=offset)
yields:
$ python3 test.py
Traceback (most recent call last):
File "test.py", line 7, in <module>
devices = nb.dcim.get_devices(limit=0, offset=offset)
File "/home/caseyp/git/python-netbox/netbox/dcim.py", line 255, in get_devices
return self.netbox_con.get('/dcim/devices/', **kwargs)
File "/home/caseyp/git/python-netbox/netbox/connection.py", line 85, in get
'&'.join('{}={}'.format(key, urllib.parse.quote(val)) for key, val in kwargs.items()), limit)
File "/home/caseyp/git/python-netbox/netbox/connection.py", line 85, in <genexpr>
'&'.join('{}={}'.format(key, urllib.parse.quote(val)) for key, val in kwargs.items()), limit)
File "/usr/lib/python3.6/urllib/parse.py", line 825, in quote
return quote_from_bytes(string, safe)
File "/usr/lib/python3.6/urllib/parse.py", line 850, in quote_from_bytes
raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes
I am not sure on how to call the api with positional args
Have been using this with Netbox v2.8 to successfully sync and manage a number of resources from AWS and other providers, but looking to upgrade to v2.10 and it seems that support is lacking for certain features.
Would be nice if the README noted which Netbox versions and/or features were supported, and by which version of python-netbox.
In particular v2.10 introduced support for bulk updates/deletes to the Netbox API, and a new implementation of custom fields which seems to be missing from python-netbox:
https://netbox.readthedocs.io/en/stable/release-notes/version-2.10/
Our netbox runs on https://$server/netbox/, so the api is at .../netbox/api/. This is currently not possible with the way python-netbox constructs its url:
python-netbox/netbox/connection.py
Line 19 in ada9ae1
Netbox has a _choices
endpoint, which is currently not supported by python-netbox. With access to the choices mappings of all assets it would be able to get the name-to-id mapping for e.g. assest status.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.