-
Hello, I would like to create a script that will fetch all devices matching a particular device model, with their interfaces (Mac Addresses) and IP address information (from the Netbox API) it seems like the I was able to achieve my goal by nesting the following API calls:
As you can see performance is very poor as I need to make 2 API calls per device ... But I have 300+ of devices and I need to run the script as fast as possible So far I failed to find a solution to get IP / MAC addresses associated to a device without using multiple API calls. I am missing something or is it not possible to achieve this with a single API call? Thanks ! Netbox version: 3.4.3 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
With the REST interface you have make the three different API calls that you've listed to get this data, actually a few more if you want to get FHRP groups associated with the device and IPs associated with the FHRP groups, it's not achievable with a single call, however you can get all the device_id records from the first call and query for multiple devices at once when looking up interface data, eg. /dcim/interface/device_id=1234&device_id=2345&device_id=5678 which can greatly increase the efficiency of the lookup. Also adjusting ?limit=500 or ?limit=0 to return data in larger batches can increase efficiency as well as ?brief=1 to return just the name and id of a row for a query if you don't need all the other fields to be joined in. If you want to work from the IP-Address data (using the assigned_object field to get the device/interface name for templating) you could just get brief device and interface data just to get the ID numbers to query in the ip-addresses data.
There is also a GraphQL API interface for making queries which might be able to do more of this in one shot, but I'm not an expert in graphql and find the docs a bit obtuse. This is an example of something I am using in Ansible to get the list of IPs assigned to a node.
{
device_ips: ip_address_list(device:"$some_device_name") { id address}
vm_ips: ip_address_list(virtual_machine:"$some_device_name") { id address}
fhrp_ids: fhrp_group_assignment_list(device: "$some_device_name") { group { id } }
}
{
fhrp_ips: ip_address_list:(fhrpgroup_id: ["1234", "4567", "5678"]) { id address }
}
—
Mark Tinberg ***@***.***>
Division of Information Technology-Network Services
University of Wisconsin-Madison
…________________________________
From: Achille Myette ***@***.***>
Sent: Monday, August 7, 2023 6:29 AM
To: netbox-community/netbox ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [netbox-community/netbox] How to get interfaces and IPs associated to a Device (Discussion #13397)
Hello,
I would like to create a script that will fetch all devices matching a particular device model, with their interfaces (Mac Addresses) and IP address information.
it seems like the /api/dcim/devices/?model=<my_device_model> endpoint does not return any interface or IP address associated to them.
I was able to achieve my goal by nesting the following API calls:
* call /api/dcim/devices/?model=<my_device_model> and extract all the <device_name>
* call api/dcim/interfaces/?device=<device_name> and extract all the <interface_id>
* call /api/ipam/ip-addresses/?interface_id=<interface_id> to get the IP Addresses
As you can see performance is very poor as I need to make 2 API calls per device ... But I have 300+ of devices and I need to run the script as fast as possible
So far I failed to find a solution to get IP / MAC addresses associated to a device without using multiple API calls.
I am missing something or is it not possible to achieve this with a single API call?
Thanks !
—
Reply to this email directly, view it on GitHub<#13397>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAS7UM67XE7WBCDH5E6TM7LXUDGQVANCNFSM6AAAAAA3G3ESYM>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the GraphQL suggestion. I was able to acheive what I wanted with a single api call. Query
Result
|
Beta Was this translation helpful? Give feedback.
Thanks for the GraphQL suggestion. I was able to acheive what I wanted with a single api call.
Query
Result