<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Export switch configuration using Python script in Network Platform API</title>
    <link>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447462#M7390</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Newbie looking some help with Python/Meraki API&lt;/P&gt;&lt;P&gt;Trying to get a switch configuration exported to an Excel sheet to then re-import into another network/Orgs, having issues with the line  "sp_data['PortId'].append(port['number'])" and with the line&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;"sp_data['Access Policy Number'].append(port['accessPolicyNumber'])". &lt;/SPAN&gt;&lt;SPAN&gt;I get a KeyError: 'number'/ KeyError: 'accessPolicyNumber'..... not sure how to solve these&lt;BR /&gt;&lt;BR /&gt;When I remove both of these lines and update the sp_data array the csv exports fine.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE class="lia-code-sample language-python"&gt;&lt;CODE&gt;import os
import requests             # handles api requests in python
import pandas as pd         # handles dataframes in python
from getpass import getpass # prompts the user for input without echoing

MERAKI_API_KEY = 'X-Cisco-Meraki-API-Key'
MERAKI_API_VALUE = os.environ.get("MERAKI_API_KEY")

serial = input('Enter the serial number of the switch: ')

# Call switch port API
switch_req = requests.get('https://api.meraki.com/api/v1/devices/' + serial +'/switch/ports', 
                            headers = {MERAKI_API_KEY : MERAKI_API_VALUE})

# Check to ensure correct API response
if switch_req.status_code != 200:
    raise Exception(f'HTTP response was {switch_req.status_code}, and 200 was expected. ' +
                    'Please ensure your Meraki API Value and switch serial number are correct.')
else:
    # Read json response
    switch_ports = switch_req.json()

# Build dictionary to organize switch port info
sp_data = {'Switch' : [], 'PortId' : [], 'Name' : [], 'Tags' : [], 'Enabled' : [], 'PoE Enabled' : [],
           'Type' : [], 'VLAN' : [], 'Voice VLAN' : [], 'Allowed VLANs' : [], 'Isolation Enabled' : [],
           'RSTP Enabled': [], 'STP Guard': [], 'Access Policy Number' : [], 'Link Negotiation' : []}

# Iterate over the switch ports to fill dictionary
for port in switch_ports:
    sp_data['Switch'].append(serial)
    sp_data['Name'].append(port['name'])
    sp_data['PortId'].append(port['number'])
    sp_data['Tags'].append(port['tags'])
    sp_data['Enabled'].append(port['enabled'])
    sp_data['PoE Enabled'].append(port['poeEnabled'])
    sp_data['Type'].append(port['type'])
    sp_data['VLAN'].append(port['vlan'])
    sp_data['Voice VLAN'].append(port['voiceVlan'])
    sp_data['Allowed VLANs'].append(port['allowedVlans'])
    sp_data['Isolation Enabled'].append(port['isolationEnabled'])
    sp_data['RSTP Enabled'].append(port['rstpEnabled'])
    sp_data['STP Guard'].append(port['stpGuard'])
    sp_data['Access Policy Number'].append(port['accessPolicyNumber'])
    sp_data['Link Negotiation'].append(port['linkNegotiation'])

# Build switch port dataframe
switch_port_df = pd.DataFrame(data = sp_data)


# Write dataframe to csv
switch_port_df.to_csv(path_or_buf = serial + '_ports.csv', index = False)

print(f'The switch port information has been stored in {serial}_ports.csv')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 03 Oct 2023 20:29:51 GMT</pubDate>
    <dc:creator>MohaLeen1</dc:creator>
    <dc:date>2023-10-03T20:29:51Z</dc:date>
    <item>
      <title>Export switch configuration using Python script</title>
      <link>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447462#M7390</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Newbie looking some help with Python/Meraki API&lt;/P&gt;&lt;P&gt;Trying to get a switch configuration exported to an Excel sheet to then re-import into another network/Orgs, having issues with the line  "sp_data['PortId'].append(port['number'])" and with the line&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;"sp_data['Access Policy Number'].append(port['accessPolicyNumber'])". &lt;/SPAN&gt;&lt;SPAN&gt;I get a KeyError: 'number'/ KeyError: 'accessPolicyNumber'..... not sure how to solve these&lt;BR /&gt;&lt;BR /&gt;When I remove both of these lines and update the sp_data array the csv exports fine.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE class="lia-code-sample language-python"&gt;&lt;CODE&gt;import os
import requests             # handles api requests in python
import pandas as pd         # handles dataframes in python
from getpass import getpass # prompts the user for input without echoing

MERAKI_API_KEY = 'X-Cisco-Meraki-API-Key'
MERAKI_API_VALUE = os.environ.get("MERAKI_API_KEY")

serial = input('Enter the serial number of the switch: ')

# Call switch port API
switch_req = requests.get('https://api.meraki.com/api/v1/devices/' + serial +'/switch/ports', 
                            headers = {MERAKI_API_KEY : MERAKI_API_VALUE})

# Check to ensure correct API response
if switch_req.status_code != 200:
    raise Exception(f'HTTP response was {switch_req.status_code}, and 200 was expected. ' +
                    'Please ensure your Meraki API Value and switch serial number are correct.')
else:
    # Read json response
    switch_ports = switch_req.json()

# Build dictionary to organize switch port info
sp_data = {'Switch' : [], 'PortId' : [], 'Name' : [], 'Tags' : [], 'Enabled' : [], 'PoE Enabled' : [],
           'Type' : [], 'VLAN' : [], 'Voice VLAN' : [], 'Allowed VLANs' : [], 'Isolation Enabled' : [],
           'RSTP Enabled': [], 'STP Guard': [], 'Access Policy Number' : [], 'Link Negotiation' : []}

# Iterate over the switch ports to fill dictionary
for port in switch_ports:
    sp_data['Switch'].append(serial)
    sp_data['Name'].append(port['name'])
    sp_data['PortId'].append(port['number'])
    sp_data['Tags'].append(port['tags'])
    sp_data['Enabled'].append(port['enabled'])
    sp_data['PoE Enabled'].append(port['poeEnabled'])
    sp_data['Type'].append(port['type'])
    sp_data['VLAN'].append(port['vlan'])
    sp_data['Voice VLAN'].append(port['voiceVlan'])
    sp_data['Allowed VLANs'].append(port['allowedVlans'])
    sp_data['Isolation Enabled'].append(port['isolationEnabled'])
    sp_data['RSTP Enabled'].append(port['rstpEnabled'])
    sp_data['STP Guard'].append(port['stpGuard'])
    sp_data['Access Policy Number'].append(port['accessPolicyNumber'])
    sp_data['Link Negotiation'].append(port['linkNegotiation'])

# Build switch port dataframe
switch_port_df = pd.DataFrame(data = sp_data)


# Write dataframe to csv
switch_port_df.to_csv(path_or_buf = serial + '_ports.csv', index = False)

print(f'The switch port information has been stored in {serial}_ports.csv')&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt; Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2023 20:29:51 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447462#M7390</guid>
      <dc:creator>MohaLeen1</dc:creator>
      <dc:date>2023-10-03T20:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: Export switch configuration using Python script</title>
      <link>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447463#M7391</link>
      <description>&lt;P&gt;My guess: it is not available for each port configuration:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Greenberet_0-1696371430937.png" style="width: 400px;"&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.cisco.com/t5/image/serverpage/image-id/263180iF5A2ABE967E67126/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;So if it the Type isn't "Custom access policy", then the field will not be here. That's why you are getting the error&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2023 22:18:04 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447463#M7391</guid>
      <dc:creator>Greenberet</dc:creator>
      <dc:date>2023-10-03T22:18:04Z</dc:date>
    </item>
    <item>
      <title>Re: Export switch configuration using Python script</title>
      <link>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447464#M7392</link>
      <description>&lt;P&gt;&lt;A href="https://community.meraki.com/t5/user/viewprofilepage/user-id/33943"&gt;@Greenberet&lt;/A&gt; is correct.&lt;BR /&gt;I'd also suggest exporting as a JSON file as it's easier to import back into the switch.&lt;BR /&gt;That said, it is less readable is readability is more important.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2023 23:45:34 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447464#M7392</guid>
      <dc:creator>Brash</dc:creator>
      <dc:date>2023-10-03T23:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Export switch configuration using Python script</title>
      <link>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447465#M7393</link>
      <description>&lt;P&gt;3 main things. &lt;/P&gt;&lt;P&gt;1- No Access Policy on trunk. So  "accessPolicyType":"Open" all the time.&lt;/P&gt;&lt;P&gt;2- Access ports can be either "accessPolicyType":"Open" or "accessPolicyType":"Custom access policy"&lt;/P&gt;&lt;P&gt;3- If Open , no Access policy number. Else Access policy number = port['accessPolicyNumber']&lt;/P&gt;&lt;P&gt;if port['accessPolicyType'] == "Custom access policy":&lt;/P&gt;&lt;P&gt;    sp_data['Access Policy Number'].append(port['accessPolicyNumber'])&lt;/P&gt;&lt;P&gt;else:&lt;/P&gt;&lt;P&gt;    sp_data['Access Policy Number'].append("NA")&lt;/P&gt;&lt;P&gt;also you are missing some other configs. DAI , Accesspolicytype  ( maybe more ) &lt;/P&gt;&lt;P&gt;Hope I'm right on that one !&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2023 01:14:22 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447465#M7393</guid>
      <dc:creator>Raphael_L</dc:creator>
      <dc:date>2023-10-04T01:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Export switch configuration using Python script</title>
      <link>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447466#M7394</link>
      <description>&lt;P&gt;Applause for looking at the use case yourself, but I think a bunch of scripts already exist out there for this (try GitHub) - including this one of Phil's : &lt;A href="https://www.ifm.net.nz/cookbooks/meraki-backup.html" target="_blank" rel="nofollow noopener noreferrer"&gt;https://www.ifm.net.nz/cookbooks/meraki-backup.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2023 08:52:49 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-platform-api/export-switch-configuration-using-python-script/m-p/5447466#M7394</guid>
      <dc:creator>GreenMan</dc:creator>
      <dc:date>2023-10-04T08:52:49Z</dc:date>
    </item>
  </channel>
</rss>

