Skip to content

Instantly share code, notes, and snippets.

@rossigee
Last active May 24, 2023 05:58
Show Gist options
  • Save rossigee/08bd586a9ec4b6f8c1fc5fb0c4a3a77e to your computer and use it in GitHub Desktop.
Save rossigee/08bd586a9ec4b6f8c1fc5fb0c4a3a77e to your computer and use it in GitHub Desktop.
import os
import requests
def get_ipam_addresses(api_url, token, ca_cert):
headers = {
'Authorization': f'Token {token}',
'Content-Type': 'application/json'
}
# Fetch all IP addresses from NetBox
response = requests.get(f'{api_url}/ipam/ip-addresses/', headers=headers, verify=ca_cert)
data = response.json()
if response.status_code != 200:
print(f'Error: {data["detail"]}')
return []
ip_addresses = []
# Extract the IP addresses from the response
for result in data['results']:
ip_address = result['address']
ip_addresses.append(ip_address)
# Check if there are more pages to fetch
while data['next']:
response = requests.get(data['next'], headers=headers, verify=ca_cert)
data = response.json()
if response.status_code != 200:
print(f'Error: {data["detail"]}')
break
for result in data['results']:
ip_address = result['address']
ip_addresses.append(ip_address)
return ip_addresses
# Set your NetBox API URL and token
netbox_api_url = os.getenv("NETBOX_API_URL") + "/api"
netbox_token = os.getenv("NETBOX_API_TOKEN")
ca_cert_path = '/usr/local/share/ca-certificates/GolderRootCA.crt'
# Call the function to get the IPAM addresses
ipam_addresses = get_ipam_addresses(netbox_api_url, netbox_token, ca_cert_path)
# Print the IPAM addresses
for address in ipam_addresses:
print(address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment