How to handle file generate fw.op in python

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements

How to handle file generate fw.op in python

L2 Linker

Hi,

 

I have the following code to check the vpn status, this code execute op "show vpn flow'

I'm ussing pandevice library in my imports


def check_vpn_status(fw): #check vpn status
vpn_ike = fw.op(cmd= "show vpn flow", xml=True)
with open('vpn_status.xml', 'wb') as file:
file.write(vpn_ike)
print(type(vpn_ike))

.  and generate an xml file like this.  Exist one method how to read the file to search into file the ie state and show in the print the following tunnels are up ?. I'm stuck how to manipulate that file generated by fw.op

<response status="success"><result>
<total>1</total>
<num_ipsec>1</num_ipsec>
<IPSec>
<entry>
<peerip>12.12.12.12</peerip>
<name>Bluecoat_IPSEC:ID-1</name>
<outer-if>ethernet1/1</outer-if>
<gwid>1</gwid>
<localip>172.16.10.1</localip>
<state>init</state>
<inner-if>tunnel.10</inner-if>
<mon>off</mon>
<owner>1</owner>
<id>1</id>
</entry>
</IPSec>
<dp>dp0</dp>
<num_sslvpn>0</num_sslvpn>
</result></response>

Any advise I really appreciate.

best Regards

Andres

1 accepted solution

Accepted Solutions

L5 Sessionator

I would skip the file part of this.  If your true goal is just to see the states of things, I would take advantage of the fact that fw.op() returns an xml.etree.ElementTree object and use the python functions available to iterate over the results:

 

 

result = fw.op('show vpn flow')
for entry in result.findall('./result/IPSec/entry'):
    name = entry.find('./name')
    state = entry.find('./state')
    if name is None or state is None:
        # This should never happen, but just to code defensively...
        continue
    print('{0} has state {1}'.format(name.text, state.text))

 

 

 

 

 

 

 

View solution in original post

2 REPLIES 2

L5 Sessionator

I would skip the file part of this.  If your true goal is just to see the states of things, I would take advantage of the fact that fw.op() returns an xml.etree.ElementTree object and use the python functions available to iterate over the results:

 

 

result = fw.op('show vpn flow')
for entry in result.findall('./result/IPSec/entry'):
    name = entry.find('./name')
    state = entry.find('./state')
    if name is None or state is None:
        # This should never happen, but just to code defensively...
        continue
    print('{0} has state {1}'.format(name.text, state.text))

 

 

 

 

 

 

 

Thanks a lot !!!!!!!!! you rocks!!!

  • 1 accepted solution
  • 4174 Views
  • 2 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!