Hello Forum
I have written the following code to output Networks and switches for my organisation
import csv
import meraki
import requests
# Replace with your Meraki API Key
API_KEY = 'MYORGAPIKEY'
# Replace with your organization ID
ORG_ID = 'MYORGID'
# 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")
However when I run this it comes up with the error
Traceback (most recent call last):
File "<stdin>", line 14, in <module>
ValueError: I/O operation on closed file.
>>> print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")
Can someone please help?