Advertisement
Guest User

Untitled

a guest
Oct 31st, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. #!/usr/local/bin/python2.7
  2.  
  3. import itertools
  4. import re
  5. import sys
  6. import xml.etree.cElementTree as ET
  7.  
  8. IPSEC_CONF = '/var/etc/ipsec/ipsec.conf'
  9. PFSENSE_CONF = '/conf/config.xml'
  10. rtt_time_warn = 200
  11. rtt_time_error = 300
  12.  
  13. #Parse the XML
  14. tree = ET.parse(PFSENSE_CONF)
  15. root = tree.getroot()
  16.  
  17. #Function to find phase description by ikeid
  18. def findDescr(remoteid,ikeid):
  19.  
  20.         #Check if the parameter was sent
  21.         if not remoteid:
  22.                 return "Not found"
  23.  
  24.         #create search string. We use the "..." after the search to return the parent element of the current element.
  25.         #The reason for that is the remoteid is a sub element of phase2 element
  26.         search = "./ipsec/phase2/remoteid/[address='" + remoteid + "']..."
  27.  
  28.         for tunnel in root.findall(search):
  29.                 descr = tunnel.find('descr').text
  30.  
  31.                 #If we have only one result, we are talking about the correct tunnel
  32.                 if len(root.findall(search)) == 1:
  33.                         return descr
  34.  
  35.                 #otherwise, if we have more than 1, we have to confirm the remoteid and the ikeid
  36.                 #Case the ikeIds are the same, we got it. Case not, we pass and wait for next interation
  37.                 else:
  38.                         #Get the ikeid of this element
  39.                         ikeidElement = tunnel.find('ikeid').text
  40.                         if ikeidElement == ikeid:
  41.                                 return descr
  42.  
  43.         return "Not found"
  44.  
  45. #Function to set correct format on ikeId. Recives conIDXXX, return ID
  46. def formatIkeId(ikeid):
  47.    
  48.     #Convert list  into a string
  49.     ikeid = ikeid[0]
  50.  
  51.     #If ikeid has 8 or more positions, get the position 3 and 4
  52.     if len(ikeid) >= 8:
  53.         ikeid = ikeid[3] + ikeid[4]
  54.     else:
  55.         #Else, get only the position 3. That is because some ikeids are small
  56.         ikeid = ikeid[3]
  57.         #print "The correct ike id is ", ikeid
  58.         return ikeid
  59.  
  60. def parseConf():
  61.     reg_conn = re.compile('^conn\s((?!%default).*)')
  62.     reg_left = re.compile('.*leftid =(.*).*')
  63.     reg_right = re.compile('.*rightid =(.*).*')
  64.     reg_rightsubnet = re.compile('.*rightsubnet =(.*).*')
  65.     data = {}
  66.     with open(IPSEC_CONF, 'r') as f:
  67.         for key, group in itertools.groupby(f, lambda line: line.startswith('\n')):
  68.             if not key:
  69.                 conn_info = list(group)
  70.                 conn_tmp = [m.group(1) for l in conn_info for m in [reg_conn.search(l)] if m]
  71.                 left_tmp = [m.group(1) for l in conn_info for m in [reg_left.search(l)] if m]
  72.                 right_tmp = [m.group(1) for l in conn_info for m in [reg_right.search(l)] if m]
  73.                 rightsubnet_tmp = [m.group(1) for l in conn_info for m in [reg_rightsubnet.search(l)] if m]
  74.         if len(conn_tmp) > 0 :
  75.             if len(rightsubnet_tmp):
  76.                 rightsubnet_tmp = rightsubnet_tmp[0].lstrip() #remore spaces
  77.                 rightsubnet_tmp = rightsubnet_tmp.split("/") #Split string to get only ip, without subnet mask)
  78.                 descr = findDescr(rightsubnet_tmp[0],formatIkeId(conn_tmp))
  79.                         else:
  80.                 rightsubnet_tmp.append("Not found")
  81.         else:
  82.             descr = "Not found"
  83.                 if conn_tmp and left_tmp and right_tmp:
  84.                     data[conn_tmp[0]] = [left_tmp[0], right_tmp[0], descr]
  85.         return data
  86.  
  87. def getTemplate():
  88.     template = """
  89.        {{ "{{#TUNNEL}}":"{0}","{{#TARGETIP}}":"{1}","{{#SOURCEIP}}":"{2}","{{#DESCRIPTION}}":"{3}" }}"""
  90.  
  91.     return template
  92.  
  93. def getPayload():
  94.     final_conf = """{{
  95.    "data":[{0}
  96.    ]
  97. }}"""
  98.  
  99.     conf = ''
  100.     data = parseConf().items()
  101.     for key,value in data:
  102.         tmp_conf = getTemplate().format(
  103.             key,
  104.             value[1],
  105.             value[0],
  106.             value[2],
  107.             rtt_time_warn,
  108.             rtt_time_error
  109.         )
  110.         if len(data) > 1:
  111.             conf += '%s,' % (tmp_conf)
  112.         else:
  113.             conf = tmp_conf
  114.     if conf[-1] == ',':
  115.         conf=conf[:-1]
  116.     return final_conf.format(conf)
  117.  
  118. if __name__ == "__main__":
  119.     ret = getPayload()
  120.     sys.exit(ret)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement