trying to fetch switch port config into a csv not working

ammahend
Building a reputation

trying to fetch switch port config into a csv not working

getting 404 error

Failed to fetch port configurations for switch with serial ****-*****-****. Status code: 404
No port data available.
I know the key and other parameters are correct because I am able to fetch other information like Network IDs from Org

Any help is appreciated.

 

import requests
import csv


# Function to get port configurations from a Meraki switch
def get_port_config(api_key, org_id, serial) :
    url = f"https://api.meraki.com/api/v1/organizations/{org_id}/devices/{serial}/switchPorts"
    headers = {"X-Cisco-Meraki-API-Key": api_key, "Content-Type": "application/json"}
    response = requests.get(url, headers=headers)

    # Check if the response is successful (status code 200)
    if response.status_code == 200:
        return response.json()
    else:
        print(
            f"Failed to fetch port configurations for switch with serial {serial}. Status code: {response.status_code}"
        )
        return None


# Function to write port configurations to a CSV file
def write_to_csv(ports_data, filename) :
    if ports_data is None:
        print("No port data available.")
        return

    with open(filename, "w", newline="") as csvfile:
        fieldnames = ["Port", "Enabled", "VLAN", "Type"]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for port in ports_data:
            writer.writerow(
                {
                    "Port": port.get("number", ""),
                    "Enabled": port.get("enabled", ""),
                    "VLAN": port.get("vlan", ""),
                    "Type": port.get("type", ""),
                }
            )


def main():
    api_key = "****"
    org_id = "****"
    switch_serials = [
        "****-****-****",
        "****-****-****",
    ]  # Add your switch serials here
    output_filename = "port_configurations.csv"

    for serial in switch_serials:
        ports_data = get_port_config(api_key, org_id, serial)
        write_to_csv(ports_data, f"{serial}_{output_filename}")


if __name__ == "__main__":
    main()
5 Replies 5
ammahend
Building a reputation

If I am making a simple call from postman, I am still getting 404 error

David_Jirku
Meraki Employee
Meraki Employee

Don't post your serial numbers in posts.

 

I don't think that your endpoint is valid:

/api/v1/organizations/{org_id}/devices/{serial}/switchPorts

 

Try using one of these: https://developer.cisco.com/meraki/api-v1/search/?q=switchPorts or https://developer.cisco.com/meraki/api-v1/get-device-switch-ports/

 

 

You can also use the Meraki Python library to help with error handling.

 

 

ammahend
Building a reputation

I realized and fixed it earlier but I can't even get a simple GET from postman for that url. I get the same 404 error.

when I try other GET requests like for getting Network_ID  it works fine.

ammahend
Building a reputation

never mind, syntax issue, thanks for the help.

DarrenOC
Kind of a big deal
Kind of a big deal

Just to reiterate the above - please remove your serial number from the post.

Darren OConnor | doconnor@resalire.co.uk
https://www.linkedin.com/in/darrenoconnor/

I'm not an employee of Cisco/Meraki. My posts are based on Meraki best practice and what has worked for me in the field.
Get notified when there are additional replies to this discussion.