#!/usr/bin/python import cgi,re,sys def portmap(x): if x == '0': return "all" else: return x def namemap(x,src,dest): return x.replace(src, dest) def process(inp,src,dst): input = str(inp).split("\n") re_content = re.compile("^.*LC_P\s*(\d+\.\d+.\d+.\d+)\s+(\d+)\s+(\w+)",re.IGNORECASE) re_service = re.compile("^.*DEST\s*(\d+\.\d+.\d+.\d+)\s+(\d+)",re.IGNORECASE) re_howmany = re.compile("^.*This source has (\d+) destinations.*",re.IGNORECASE) re_lastresort = re.compile("^.*LAST\_RESORT.*", re.IGNORECASE) cnt = 0 services = [] print "Input has %d lines\n\n\n" % len(input) for line in input: cnt += 1 try: howmanysource = int(re_howmany.match(line).group(1)) (content_ip, content_port, content_pname) = re_content.match(input[cnt]).groups() ### start printing the stuff # vips with changed ip addresses and ports as needed print """\ncontent %s-%s vip address %s""" % (namemap(content_ip,src,dst), \ portmap(content_port), \ namemap(content_ip,src,dst)) for x in range(howmanysource + 1): # print out each service, and if it's last service for http/https content rule # output it as a sorry server try: (serviceip, serviceport) = re_service.match(input[cnt+x]).groups() # append to list, for later printout services.append((serviceip,serviceport)) # check if that is sorry server (last from destinations) #if x == howmanysource: isSorry = re_lastresort.match(input[cnt+x]) if isSorry: (sorryip,sorryport) = (serviceip,serviceport) if content_port != "0": print " primarySorryServer %s-%s" % (sorryip,sorryport) print " port %s" % content_port else: print " primarySorryServer %s-all" % (sorryip) else: if serviceport == "0": print " add service %s-all" % serviceip else: print " add service %s-%s" % (serviceip,serviceport) except (TypeError,AttributeError): continue print " active" except (TypeError,AttributeError): continue #sys.exit(-1) for (servip, servport) in services: print "" if servport == '0': print """service %s-all ip address %s keepalive type icmp active""" % (servip,servip) ### # services 80 and 443 are in separate if conditionals, just for future use # for example if customer would like to use http get/arrowpoint, etc. ### elif servport == '80': print """service %s-80 ip address %s keepalive type tcp keepalive port 80 active""" % (servip,servip) elif servport == '443': print """service %s-443 ip address %s keepalive type tcp keepalive port 443 active""" % (servip,servip) else: print """service %s-%s ip address %s keepalive type tcp keepalive port %s active""" % (servip,servport,servip,servport) def main(): form = cgi.FieldStorage() if not form: printform() else: #print "zastapione: %s" % form['wpis'].value if form.has_key('content') and form.has_key('src') and form.has_key('dst'): print "Content-type: text/plain\n" process(form['content'].value, form['src'].value, form['dst'].value) else: printform() def printform(): print "Content-type: text/html\n" print """