自己寫的pythonRegex

來源:互聯網
上載者:User

import re

 # Validate logs from admd. Have "from"  and have  comma. It mean both "client IP" and "denied reason" is given by appliance.
def validate_re1():
    print 'The following test case should succeed: '
    regex = 'user\s+\[(\S+)\]\s+from\s+(\d+\.\d+\.\d+\.\d+)\s+(\S.*),\s+(\S.*)'
   
    re_match = re.search(regex, 'ADM auth Firewall user [hama@qanet.net] from 10.0.1.2 Error, Reason - Ldap binding not successful')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4).replace('Reason - ', '')
    re_match = re.search(regex, 'ADM auth Firewall user [jason@RADIUS] from 10.139.44.131 Error, Reason - Recv timeout')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4).replace('Reason - ', '')
    re_match = re.search(regex, 'ADM auth Firewall user [tiger@Firebox-DB] from 10.139.44.131 Rejected, Password Incorrect')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
    re_match = re.search(regex, 'ADM auth Firewall user [123_123@Firebox-DB] from 10.139.44.131 Rejected, User Not Found')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
    re_match = re.search(regex, 'ADM auth Firewall user [do_ha_ha@tiger.com] from 10.0.1.2 Error, Reason - Ldap binding not successful')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
    re_match = re.search(regex, 'ADM auth Firewall user [do_ha_ha@tiger.com] from 10.0.1.2 Rejected, Exceeded login limit')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
    print '--------------------------------------'

 
 # Validate logs from admd. Have "from"  but do not have  comma   
def validate_re2():
    print 'The following test case should succeed: '
    regex = 'user\s+\[(\S+)\]\s+from\s+(\d+\.\d+\.\d+\.\d+)\s+(\S.*)'
   
    re_match = re.search(regex, 'ADM auth Firewall user [yyyyyyyyyyyyy@RADIUS] from 10.0.1.2 Rejected')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + "N/A"
    re_match = re.search(regex, 'ADM auth Firewall user [hama@qanet.net] from 10.0.1.2 Rejected')
    print re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + "N/A"
    print '--------------------------------------'
 
 # Validate logs from admd.  Have no "from"  but have  comma 
def validate_re3():
    print 'The following test case should succeed: '
    regex = 'user\s+\[(\S+)\]+(\s*\S*),\s+(\S.*)'
   
    re_match = re.search(regex, 'ADM auth Firewall user [yyyyyyyyyyyyy@RADIUS] Error, radius auth method ytyty not supported')
    print re_match.group(1) + "|    |" + "N/A" + "|    |" + re_match.group(3)
   
    re_match = re.search(regex, 'ADM auth user [jason@RADIUS], both primary and secondary servers are down')
    print re_match.group(1) + "|    |" + "N/A" + "|    |" + re_match.group(3)
    print '--------------------------------------'
   
#  Validate logs from sessiond.  
def validate_re4():
    print 'The following test case should succeed: '
#    regex = 'user\s+(\S+)\s+from\s+(\d+\.\d+\.\d+\.\d+)\s+(\S.*)'
#    re_match = re.search(regex, 'Firewall user frank@RADIUS from 10.139.44.131 rejected 111aaa')
    log1 = 'Management user admin from 172.26.0.107 rejected - admin have login.'
    log2 = 'Firewall user jerry@Firebox-DB from 10.139.36.83 rejected - Exceeded authenticated users limit'
    log3 = "Firewall user andy@Firebox-DB from 10.0.1.2 rejected - Unspecified" 
    regex = 'user\s+(\S+)\s+from\s+(\d+\.\d+\.\d+\.\d+)\s+(\S.*)\s-\s+(\S.*)'
   
    if  log1.find('rejected')!= -1 and (log1.find('Management') != -1 or log1.find('Exceeded authenticated users limit')!= -1):
        re_match = re.search(regex, log1)
        print "log1--->"+re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
       
    if  log2.find('rejected')!= -1 and (log2.find('Management') != -1 or log2.find('Exceeded authenticated users limit')!= -1):
        re_match = re.search(regex, log2)
        print  "log2--->"+re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
       
    print 'The following test case should fail: '
    if  log3.find('rejected')!= -1 and (log3.find('Management') != -1 or log3.find('Exceeded authenticated users limit')!= -1):
        re_match = re.search(regex, log3)
        print  "log3--->"+ re_match.group(1) + "|    |" + re_match.group(2) + "|    |" + re_match.group(4)
   
   
if __name__ == "__main__":     
        
     #Should  match  
     validate_re1()
     validate_re2()
     validate_re3()
     validate_re4()

     print '----------------------'

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.