Exporting Agent Data in CSV Using the NetBeez Agents Collection API in Python

The Agents Collection API provided by NetBeez allows us to retrieve extensive data about our network and remote worker agents. This tutorial demonstrates how to retrieve information for all agents using pagination and output it in CSV format in stdout. The fields we focus on include: name, IP address, public IP address, DNS servers, gateway, DHCP flag, MAC address, version, ISP name, ISP ASN, and Network Interface Key. Note that an agent may have multiple network interfaces - in that case each network interface will be represented in a separate agent row.

Here’s the Python code for that:

import csv
import requests
import sys

API_URL = "https://[HOSTNAME]agents" # Replace with your server's hostname
API_KEY = "YOUR_API_KEY" # Replace with your API key

headers = {"Authorization": f"Bearer {API_KEY}"}

def get_network_interfaces(network_interface_ids):
    interfaces = []
    for item in data['included']:
        if item['id'] in network_interface_ids and item['type'] == "network_interface":
    return interfaces

writer = csv.writer(sys.stdout)
writer.writerow(["Name", "Network Interface Key", "IP Address", "Public IP Address", "DNS Server 1", "DNS Server 2", "Gateway", "DHCP", "MAC Address", "Version", "ISP Name", "ISP ASN"])

page_number = 1
records_per_page = 25

while True:
    params = {"page[offset]": page_number, "page[limit]": records_per_page, "type":"beta"}
    response = requests.get(API_URL, headers=headers, params=params, verify=False)

    data = response.json()
    for agent_data in data['data']:
        attributes = agent_data['attributes']
        name = attributes.get("name")
        version = attributes.get("software_version")
        isp_name = attributes.get("isp_name")
        isp_asn = attributes.get("isp_asn")

        network_interface_ids = [network_interface['id'] for network_interface in agent_data['relationships']['network_interfaces']['data']]
        network_interfaces_data = get_network_interfaces(network_interface_ids)

        for network_interface_data in network_interfaces_data:
            network_interface_key = network_interface_data['attributes'].get("key")
            ip_address = network_interface_data['attributes'].get("ip_address")
            public_ip_address = network_interface_data['attributes'].get("external_ip")
            dns_server1 = network_interface_data['attributes'].get("dns_server1")
            dns_server2 = network_interface_data['attributes'].get("dns_server2")
            gateway = network_interface_data['attributes'].get("gateway")
            dhcp = network_interface_data['attributes'].get("dhcp")
            mac_address = network_interface_data['attributes'].get("mac")

            writer.writerow([name, network_interface_key, ip_address, public_ip_address, dns_server1, dns_server2, gateway, dhcp, mac_address, version, isp_name, isp_asn])

    if len(data['data']) < records_per_page:

    page_number += 1

In this script, we iterate as many times as needed to get all the agents by page. On each iteration we construct each agent’s rows. The get_network_interfaces function fetches all the network interface objects included with an agent. Finally, we write all the required data to stdout in CSV format.

Please replace “YOUR_API_KEY” with your actual API key in the headers dictionary. The pagination parameters in the API request are set to retrieve 25 records per page.

This script outputs the data to stdout, which you can easily redirect to a CSV file when running the script.

I hope this was helpful, and please feel free to ask if you have further questions!

1 Like