Perhaps you have an indentation error as your code works for me, only thing I changed was the curly braces {} to square ones [] when you are doing the writer.writerow within the devices for loop. import csv
import meraki
import requests
# Replace with your Meraki API Key
API_KEY = 'dontlookatmykey'
# Replace with your organization ID
ORG_ID = 'xxxx'
# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)
# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)
# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])
# Iterate through networks
for network in networks:
NetworkID = network['id']
NetworkName = network['name']
# Get devices in each network
devices = dashboard.networks.getNetworkDevices(NetworkID)
for device in devices:
DeviceName = device.get('name', 'N/A')
Serial = device.get('serial')
Model = device.get('model', 'N/A')
MAC = device.get('mac', 'N/A')
IP = device.get("lanIp", 'N/A')
writer.writerow([
NetworkID,
NetworkName,
DeviceName,
Serial,
Model,
MAC,
IP
])
print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")
... View more