<?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 Python script for generating CSV containing MACs &amp;amp; description in specific endpoint group in Network Access Control</title>
    <link>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899556#M470943</link>
    <description>&lt;P&gt;Using python I have created a script to essentially allow our imaging team to gather a list of MACs/description from a specific endpoint group.&amp;nbsp; I have the script and proper calls 95% working for what I want it to do.&amp;nbsp; However, I am in the process of figuring out two things:&lt;/P&gt;&lt;P&gt;1 - How to write the output to a csv without overwriting rows.&amp;nbsp; The append method seems to work when tweaking code.&amp;nbsp; However, I am at a loss for figuring out how to use headers with append.&lt;/P&gt;&lt;P&gt;2 - Since the endpoint group in ISE is smaller (&amp;lt;100 MACs) I am able to write code to reference the endpoint group href in separate json fields for the next page etc. My question is how would one scale if the endpoint group has more than a few pages of output?&lt;/P&gt;&lt;P&gt;For 2, I was able to run specific curl commands to gather the appropriate information I needed and the last output provided here:&lt;/P&gt;&lt;P&gt;"nextPage" : {&lt;BR /&gt;"rel" : "next",&lt;BR /&gt;"href" : "&lt;A href="https://128.47.3.201:9060/ers/config/endpoint?filter=groupId.EQ.a1649530-6840-11e9-aa97-0242f38bcc8a&amp;amp;page=2" target="_blank" rel="noopener"&gt;https://XXXXXX:9060/ers/config/endpoint?filter=groupId.EQ.a164XXX6840XXX7-0242f38bcc8a&amp;amp;page=2&lt;/A&gt;",&lt;BR /&gt;"type" : "application/xml"&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;which led me to the scaling question.&amp;nbsp; For part 1, here is a snippet:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;print "Total Number of Endpoints in Group:",endPoints['SearchResult']['total']&lt;BR /&gt;print "---------------------------------------"&lt;/P&gt;&lt;P&gt;for MAC in endPoints ["SearchResult"]["resources"]:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; temp1 = MAC['name']&lt;BR /&gt;&amp;nbsp; &amp;nbsp; try:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp2 = MAC['description']&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; with open ('Img_Grp_Macs.csv', 'wb') as csvfile:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ##headers = ['MAC Address','Description']&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; #filewriter.writerow(['MAC Address', 'Description'])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter.writerow([temp1, temp2])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print "MAC Address:", temp1, "||", "Description:", temp2&lt;BR /&gt;&amp;nbsp; &amp;nbsp; except KeyError:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; with open ('Img_Grp_Macs.csv', 'wb') as csvfile:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; #filewriter.writerow(['MAC Address', 'Description'])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter.writerow([temp1, temp2])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; print "MAC Address:", temp1, "||", "Description:"""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; continue&lt;/P&gt;&lt;P&gt;Excuse the spacing from copy/paste.&amp;nbsp; The output to screen works and looks something like this:&lt;/P&gt;&lt;P&gt;Total Number of Endpoints in Group: 64&lt;BR /&gt;---------------------------------------&lt;BR /&gt;MAC Address: x:xxxxxxx:x || Description: test123&lt;BR /&gt;MAC Address: x:xxxx:x || Description: test&lt;/P&gt;&lt;P&gt;....and so on&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jul 2019 19:58:03 GMT</pubDate>
    <dc:creator>Mike.Cifelli</dc:creator>
    <dc:date>2019-07-29T19:58:03Z</dc:date>
    <item>
      <title>Python script for generating CSV containing MACs &amp; description in specific endpoint group</title>
      <link>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899556#M470943</link>
      <description>&lt;P&gt;Using python I have created a script to essentially allow our imaging team to gather a list of MACs/description from a specific endpoint group.&amp;nbsp; I have the script and proper calls 95% working for what I want it to do.&amp;nbsp; However, I am in the process of figuring out two things:&lt;/P&gt;&lt;P&gt;1 - How to write the output to a csv without overwriting rows.&amp;nbsp; The append method seems to work when tweaking code.&amp;nbsp; However, I am at a loss for figuring out how to use headers with append.&lt;/P&gt;&lt;P&gt;2 - Since the endpoint group in ISE is smaller (&amp;lt;100 MACs) I am able to write code to reference the endpoint group href in separate json fields for the next page etc. My question is how would one scale if the endpoint group has more than a few pages of output?&lt;/P&gt;&lt;P&gt;For 2, I was able to run specific curl commands to gather the appropriate information I needed and the last output provided here:&lt;/P&gt;&lt;P&gt;"nextPage" : {&lt;BR /&gt;"rel" : "next",&lt;BR /&gt;"href" : "&lt;A href="https://128.47.3.201:9060/ers/config/endpoint?filter=groupId.EQ.a1649530-6840-11e9-aa97-0242f38bcc8a&amp;amp;page=2" target="_blank" rel="noopener"&gt;https://XXXXXX:9060/ers/config/endpoint?filter=groupId.EQ.a164XXX6840XXX7-0242f38bcc8a&amp;amp;page=2&lt;/A&gt;",&lt;BR /&gt;"type" : "application/xml"&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;which led me to the scaling question.&amp;nbsp; For part 1, here is a snippet:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;print "Total Number of Endpoints in Group:",endPoints['SearchResult']['total']&lt;BR /&gt;print "---------------------------------------"&lt;/P&gt;&lt;P&gt;for MAC in endPoints ["SearchResult"]["resources"]:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; temp1 = MAC['name']&lt;BR /&gt;&amp;nbsp; &amp;nbsp; try:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp2 = MAC['description']&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; with open ('Img_Grp_Macs.csv', 'wb') as csvfile:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ##headers = ['MAC Address','Description']&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; #filewriter.writerow(['MAC Address', 'Description'])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter.writerow([temp1, temp2])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print "MAC Address:", temp1, "||", "Description:", temp2&lt;BR /&gt;&amp;nbsp; &amp;nbsp; except KeyError:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; with open ('Img_Grp_Macs.csv', 'wb') as csvfile:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_MINIMAL)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; #filewriter.writerow(['MAC Address', 'Description'])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; filewriter.writerow([temp1, temp2])&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; print "MAC Address:", temp1, "||", "Description:"""&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; continue&lt;/P&gt;&lt;P&gt;Excuse the spacing from copy/paste.&amp;nbsp; The output to screen works and looks something like this:&lt;/P&gt;&lt;P&gt;Total Number of Endpoints in Group: 64&lt;BR /&gt;---------------------------------------&lt;BR /&gt;MAC Address: x:xxxxxxx:x || Description: test123&lt;BR /&gt;MAC Address: x:xxxx:x || Description: test&lt;/P&gt;&lt;P&gt;....and so on&lt;/P&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 19:58:03 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899556#M470943</guid>
      <dc:creator>Mike.Cifelli</dc:creator>
      <dc:date>2019-07-29T19:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Python script for generating CSV containing MACs &amp; description in specific endpoint group</title>
      <link>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899699#M470946</link>
      <description>&lt;P&gt;I know this doesn't answer your questions as I am still learning Python myself, but FYI you can increase your counts per request to 100 by adding "&amp;amp;size=100" to the end of your query.&amp;nbsp; That will at least cut down the number of pages to call for larger groups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I never did a filter by groupID before so that is pretty nice.&amp;nbsp; I noted that for future reference.&amp;nbsp; I test the load in my Python shell and was able to cycle through the data (in JSON format).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good stuff.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 04:12:13 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899699#M470946</guid>
      <dc:creator>paul</dc:creator>
      <dc:date>2019-07-30T04:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: Python script for generating CSV containing MACs &amp; description in specific endpoint group</title>
      <link>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899726#M470947</link>
      <description>&lt;P&gt;At least I did it this way:&lt;/P&gt;&lt;P&gt;1.) Initialize an empty result dictionary first (e.g. &lt;SPAN&gt;__result_content &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; {} &lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;2.) Initialize a variable, which contains the next URL reference (__nextURL). Initially this variable is populated with your initial API call&lt;/P&gt;&lt;P&gt;3.) Build a loop (e.g. while &lt;SPAN&gt;__nextURL &lt;/SPAN&gt;&lt;SPAN&gt;!=&lt;/SPAN&gt; &lt;SPAN&gt;None&lt;/SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;FONT color="#333333"&gt;4.) &lt;FONT color="#000000"&gt;API get request inside the loop.... Get the results as a dictionary&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;FONT color="#333333"&gt;&lt;FONT color="#000000"&gt;5.) Append the results to the empty dictionary from the beginning (1)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;FONT color="#333333"&gt;&lt;FONT color="#000000"&gt;6.) From the results, check if the nextPage element is present in the results. If yes, update the __nextURL variable to this value. If not, set the __nextURL to "None".&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;FONT color="#333333"&gt;&lt;FONT color="#000000"&gt;==&amp;gt; If there is a next page, the loop runs again and updates the dictionary.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;FONT color="#333333"&gt;&lt;FONT color="#000000"&gt;At the end of the day, the result dictionary contains all elements. After that you can do whatever you want with it (e.g. loop over the items and create a CSV).&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 05:23:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3899726#M470947</guid>
      <dc:creator>Johannes Luther</dc:creator>
      <dc:date>2019-07-30T05:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python script for generating CSV containing MACs &amp; description in specific endpoint group</title>
      <link>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3903873#M471047</link>
      <description>For part 2 here is the code I used to accomplish what I wanted to do with the CSV:&lt;BR /&gt;--Excluded json, global variables, &amp;amp; requests code--&lt;BR /&gt;f = open ('Img_Grp_Macs.csv','w')&lt;BR /&gt;w = csv.DictWriter(f, fieldnames = ['MAC Address','Description'])&lt;BR /&gt;w.writeheader()&lt;BR /&gt;f.close()&lt;BR /&gt;&lt;BR /&gt;for MAC in endPoints ["SearchResult"]["resources"]:&lt;BR /&gt;temp1 = MAC['name']&lt;BR /&gt;with open ('Img_Grp_Macs.csv','a') as csvfile:&lt;BR /&gt;w = csv.writer(csvfile)&lt;BR /&gt;try:&lt;BR /&gt;temp2 = MAC['description']&lt;BR /&gt;w.writerow([temp1, temp2])&lt;BR /&gt;print "MAC Address:", temp1, "||", "Description:", temp2&lt;BR /&gt;except KeyError:&lt;BR /&gt;w.writerow([temp1, temp2])&lt;BR /&gt;print "MAC Address:", temp1, "||", "Description:"""&lt;BR /&gt;continue&lt;BR /&gt;with open ('Img_Grp_Macs.csv','a') as csvfile:&lt;BR /&gt;w = csv.writer(csvfile)&lt;BR /&gt;TOTAL = endPoints['SearchResult']['total']&lt;BR /&gt;Works like a charm! Cheers!&lt;BR /&gt;</description>
      <pubDate>Tue, 06 Aug 2019 14:48:14 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3903873#M471047</guid>
      <dc:creator>Mike.Cifelli</dc:creator>
      <dc:date>2019-08-06T14:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: Python script for generating CSV containing MACs &amp; description in specific endpoint group</title>
      <link>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3904765#M471053</link>
      <description>Try this for 1 :&lt;BR /&gt;=============================================================&lt;BR /&gt;print "Total Number of Endpoints in Group:",endPoints['SearchResult']['total']&lt;BR /&gt;print "---------------------------------------"&lt;BR /&gt;print "MAC Address, Description\n"&lt;BR /&gt;&lt;BR /&gt;for MAC in endPoints ["SearchResult"]["resources"]:&lt;BR /&gt;    temp1 = MAC['name']&lt;BR /&gt;    try:&lt;BR /&gt;        temp2 = MAC['description']&lt;BR /&gt;                print temp1 +","+ temp2 + "\n"&lt;BR /&gt;=============================================================== &lt;BR /&gt;While running the script just say &amp;gt;&amp;gt; MacGroup.csv for example : python testscript.py &amp;gt;&amp;gt; MacGroup.csv &lt;BR /&gt;&lt;BR /&gt;For 2, &lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/15810"&gt;@paul&lt;/a&gt; 's  solution should do&lt;BR /&gt;</description>
      <pubDate>Wed, 07 Aug 2019 18:02:13 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-access-control/python-script-for-generating-csv-containing-macs-amp-description/m-p/3904765#M471053</guid>
      <dc:creator>Surendra</dc:creator>
      <dc:date>2019-08-07T18:02:13Z</dc:date>
    </item>
  </channel>
</rss>

