<?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 Re: How to parse op(cmd) results in Automation/API Discussions</title>
    <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319146#M2251</link>
    <description>&lt;P&gt;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/46544"&gt;@gfreeman&lt;/a&gt;&amp;nbsp;i tried your code but it did not work for me. I&amp;nbsp;tried to check if event the for loop gets executed it did not print 'TEST'.&lt;/P&gt;&lt;P&gt;ET does not seem to like tostring/findall&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;fw = Firewall('10.10.10.10',api_key='abcdefgh')
result = fw.op(cmd='show global-protect-gateway current-user')
print(type('result\n'))
# If you wanted to see the xml response as a string:
#print(ET.tostring(result, encoding='utf-8')

for elm in result.findall('./response/entry'):
    print("\nTEST\n")
    print(elm.attrib['name'] + ':')
    for e in elm:
        print(e.tag + ': ' + e.text)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 517px;"&gt;&lt;img src="https://live.paloaltonetworks.com/t5/image/serverpage/image-id/24723i5F53B3FA1C27BADB/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 519px;"&gt;&lt;img src="https://live.paloaltonetworks.com/t5/image/serverpage/image-id/24724i483AFCE4A318666A/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 28 Mar 2020 05:24:26 GMT</pubDate>
    <dc:creator>raji_toor</dc:creator>
    <dc:date>2020-03-28T05:24:26Z</dc:date>
    <item>
      <title>How to parse op(cmd) results</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/318634#M2243</link>
      <description>&lt;P&gt;I am new to python. I am able to get results using op() method but not able to display or parse them to use meaningfully.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;fw = pandevice.base.PanDevice('10.10.10.10',api_key='abcdefgh')
fwcmd = fw.op(cmd='show global-protect-gateway current-user',xml=True)

print(type(fwcmd),"\n\n")
print(fwcmd)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the result this yields is like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;class 'bytes'&amp;gt; 
b'&amp;lt;response status="success"&amp;gt;&amp;lt;result&amp;gt;\n\t&amp;lt;entry&amp;gt;\n\t\t&amp;lt;domain&amp;gt;abc&amp;lt;/domain&amp;gt;\n\t\t&amp;lt;islocal&amp;gt;no&amp;lt;/islocal&amp;gt;\n\t\t&amp;lt;username&amp;gt;xyz....................&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I would want a result that looks something like this and be able to easily operate on those results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Person ID: system
hostname: ABC-PAN
ip-address: 10.10.10.10
public-ip-address: unknown
netmask: 255.255.2255.0
default-gateway: 10.10.10.1
is-dhcp: False
ipv6-address: unknown
++&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Above results i got using&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;import pandevice
from pandevice import base

fw = pandevice.base.PanDevice('10.10.10.10',api_key='abdefgh')
jpfw = fw.show_system_info()

for p_id, p_info in jpfw.items():
    print("\nPerson ID:", p_id)

    for key in p_info:
        print(key + ':', p_info[key])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Mar 2020 04:23:01 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/318634#M2243</guid>
      <dc:creator>raji_toor</dc:creator>
      <dc:date>2020-03-26T04:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse op(cmd) results</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319017#M2249</link>
      <description>&lt;P&gt;5 days for this small piece of code...might not be the best way...but got it working as i would like it to...and can now use it possibly for something&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;import pandevice
from pandevice import base
import xml.etree.ElementTree as ET


fw = pandevice.base.PanDevice('10.10.10.10',api_key='abcdefgh')
fcmd = fw.op(cmd='show global-protect-gateway current-user',xml=True)

str_xml = str(fwcmd,encoding='utf-8')
#print(str_xml)

xml_parser = ET.XMLParser()
xml_tree = ET.ElementTree(ET.fromstring(str_xml,xml_parser))
xml_root = xml_tree.getroot()


for elem in xml_root.iter():
    if elem.tag != 'entry' and elem.tag != 'response' and elem.tag != 'result':
        print(elem.tag,': ',elem.text)
    else:
        print('')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;result looks like this where elem.tag is the string on left and elem.text is the string on right of ':'&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;domain :  abc
islocal :  no
username :  xyz
primary-username :  abc\xyz
computer :  C_NAME
client :  Microsoft Windows 10 Home , 64-bit
vpn-type :  Device Level VPN
virtual-ip :  10.1.1.1
virtual-ipv6 :  ::
public-ip :  2.2.2.2
public-ipv6 :  ::
tunnel-type :  SSL
public-connection-ipv6 :  no
login-time :  Mar.27 06:31:46
login-time-utc :  1585315906
lifetime :  7200&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 13:40:16 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319017#M2249</guid>
      <dc:creator>raji_toor</dc:creator>
      <dc:date>2020-03-27T13:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse op(cmd) results</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319019#M2250</link>
      <description>&lt;P&gt;Glad you got it working!&amp;nbsp; &lt;CODE&gt;fw.op()&lt;/CODE&gt; will return an &lt;CODE&gt;xml.etree.ElementTree&lt;/CODE&gt; by default, so turning it into a string and then having to re-parse it is a (small) waste.&amp;nbsp; You're using a PanDevice object instead of the Firewall object, so I'd recommend changing that for clarity's sake.&amp;nbsp; Then lastly you know what the result looks like, so you can use &lt;CODE&gt;xml.etree.ElementTree.findall()&lt;/CODE&gt; to get there directly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;from pandevice.firewall import Firewall
import xml.etree.ElementTree as ET

fw = Firewall('10.10.10.10', api_key='abcdefgh')
result = fw.op(cmd='show global-protect-gateway current-user')

# If you wanted to see the xml response as a string:
# print(ET.tostring(result, encoding='utf-8')

for elm in result.findall('./response/entry'):
  print(elm.attrib['name'] + ':')
  for e in elm:
    print(e.tag + ': ' + e.text)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 14:04:29 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319019#M2250</guid>
      <dc:creator>gfreeman</dc:creator>
      <dc:date>2020-03-27T14:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse op(cmd) results</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319146#M2251</link>
      <description>&lt;P&gt;&lt;a href="https://live.paloaltonetworks.com/t5/user/viewprofilepage/user-id/46544"&gt;@gfreeman&lt;/a&gt;&amp;nbsp;i tried your code but it did not work for me. I&amp;nbsp;tried to check if event the for loop gets executed it did not print 'TEST'.&lt;/P&gt;&lt;P&gt;ET does not seem to like tostring/findall&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;fw = Firewall('10.10.10.10',api_key='abcdefgh')
result = fw.op(cmd='show global-protect-gateway current-user')
print(type('result\n'))
# If you wanted to see the xml response as a string:
#print(ET.tostring(result, encoding='utf-8')

for elm in result.findall('./response/entry'):
    print("\nTEST\n")
    print(elm.attrib['name'] + ':')
    for e in elm:
        print(e.tag + ': ' + e.text)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 517px;"&gt;&lt;img src="https://live.paloaltonetworks.com/t5/image/serverpage/image-id/24723i5F53B3FA1C27BADB/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 519px;"&gt;&lt;img src="https://live.paloaltonetworks.com/t5/image/serverpage/image-id/24724i483AFCE4A318666A/image-size/large/is-moderation-mode/true?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Mar 2020 05:24:26 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319146#M2251</guid>
      <dc:creator>raji_toor</dc:creator>
      <dc:date>2020-03-28T05:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to parse op(cmd) results</title>
      <link>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319294#M2252</link>
      <description>&lt;P&gt;Oops: &lt;CODE&gt;'./response/entry'&lt;/CODE&gt; should be &lt;CODE&gt;'./result/entry'&lt;/CODE&gt; and it should work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't see what the code looks like above the print statement, but make sure you've removed &lt;CODE&gt;xml=True&lt;/CODE&gt; from the &lt;CODE&gt;fw.op()&lt;/CODE&gt;.&amp;nbsp; You want &lt;CODE&gt;xml=False&lt;/CODE&gt; (the default) because you want &lt;CODE&gt;fw.op()&lt;/CODE&gt; to return an ElementTree, not a string or bytes or whatever.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Mar 2020 06:10:14 GMT</pubDate>
      <guid>https://live.paloaltonetworks.com/t5/automation-api-discussions/how-to-parse-op-cmd-results/m-p/319294#M2252</guid>
      <dc:creator>gfreeman</dc:creator>
      <dc:date>2020-03-30T06:10:14Z</dc:date>
    </item>
  </channel>
</rss>

