2016-08-26 17 views
-4

私はこの形式のsyslogファイルを持っています。syslogのPython正規表現解析

Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: Application Version: 8.44.0 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: Run on system: host 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: Running as user: SYSTEM 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: User has admin rights: yes 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: Start Time: 2016-03-07 13:44:55 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: IP Address: 10.10.10.10 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: CPU Count: 1 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: System Type: Server 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Info: MODULE: Startup MESSAGE: System Uptime: 18.10 days 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: MODULE: InitHead MESSAGE: => Reading signature and hash files ... 
Mar 7 13:44:55 host.domain.example.net/10.10.10.10 Application: Notice: MODULE: Init MESSAGE: file-type-signatures.cfg initialized with 80 values. 
Mar 7 13:44:56 host.domain.example.net/10.10.10.10 Application: Notice: MODULE: Init MESSAGE: signatures/filename-characteristics.dat initialized with 2778 values. 
Mar 7 13:44:56 host.domain.example.net/10.10.10.10 Application: Notice: MODULE: Init MESSAGE: signatures/keywords.dat initialized with 63 values. 
Some logs ... 
Mar 7 17:42:08 host.domain.example.net/10.10.10.10 Application: Results: MODULE: Report MESSAGE: Results: 0 Alarms, 0 Warnings, 131 Notices, 2 Errors 
Mar 7 17:42:08 host.domain.example.net/10.10.10.10 Application: End: MODULE: Report MESSAGE: Begin Time: 2016-03-07 13:44:55 
Mar 7 17:42:08 host.domain.example.net/10.10.10.10 Application: End: MODULE: Report MESSAGE: End Time: 2016-03-07 17:42:07 
Mar 7 17:42:08 host.domain.example.net/10.10.10.10 Application: End: MODULE: Report MESSAGE: Scan took 3 hours 57 mins 11 secs 

「アプリケーションのバージョン」、「ユーザーが管理者権限を持っている」「システム上で実行」を抽出する方法を、「開始時間」、「IPアドレス」、「CPU数」、「システムの種類」、「 「システム稼働時間」、「終了時間」、および「アラーム」、「警告」、「通知」、「エラー」の数はPythonを使用していますか?

実際、私はPythonを初めて使っていますので、実際にどのように行うのか分かりません。私は

def finder(fname,str): 
    with open(fname, "r") as hand: 
     for line in hand: 
      line = line.rstrip() 
      if re.search(str, line): 
       return line 

)ファインダー(という名前の関数を作るために管理し、私はこれがフルラインを出力します

finder("file path","MESSAGE: IP Address") 

でそれを呼び出しますIPアドレスを持つラインを取得するために、私は取得する助けが必要そのipaddress部分、 と他の情報の残りの部分も同様です。

+0

(あなたのタイトルが示すように)場合は、正規表現を使用していますが、どのパターンを試しましたか? –

+0

あなたのコードとエラーがあれば教えてください –

+0

実際には私はPythonを初めて使っていますので、どうやってそれを行うのか分かりません。手でラインの :手としてオープン(fnameは、 "R")と :私はファインダー デフファインダー(fnameは、STR)という名前の関数を作るために管理 ライン= line.rstrip() 再場合。検索(文字列、行): 戻りライン と私は ファインダーでそれを呼び出しますIPアドレスを持つラインを取得する(ファイルパスを、「MESSAGE:IPアドレス」)これは、すべての行を出力します 、私は助けを必要とそのipaddress部分だけを取得し、その他の情報も他の行に含めます。 – Opera

答えて

0

コードを確認する前に、以下のリンクを確認してください。それはあなたを大いに助けます。

  1. re module - 使用するモジュール。このリンクには、例とともに大きな説明があります。
  2. Python Regex Tester - ここでは、正規表現とPythonで利用可能な正規表現に関連する関数をテストできます。私は以下で使用している正規表現をテストするために同じを使用している:

コードをコメントインライン

import re 
fo = open("out.txt", "r") 
#The information we need to collect. 
info_list =["Application Version", "Run on system", "User has admin rights", "Start Time", "IP Address", "CPU Count", "System Type", "System Uptime", "End Time", "Results","Begin Time"] 
for line in fo: 
    for srch_pat in info_list: 
     #First will search if the inforamtion we need is present in line or not. 
     if srch_pat in line: 
      #This will get the exact information. For e.g, version number in case of Application Version 
      regex = re.compile(r'MESSAGE:\s+%s:\s+(.*)'%srch_pat) 
      m = regex.search(line) 

      if "Results" in srch_pat: 
       #For result, this regex will get the required info 
       result_regex = re.search(r'(\d+)\s+Alarms,\s+(\d+)\s+Warnings,\s+(\d+)\s+Notices,\s+(\d+)\s+Errors',m.group(1)) 
       print 'Alarms - ',result_regex.group(1) 
       print 'Warnings - ',result_regex.group(2) 
       print 'Notices - ',result_regex.group(3) 
       print 'Errors - ',result_regex.group(4) 
      else: 
       print srch_pat,'-',m.group(1) 

と出力

C:\Users\dinesh_pundkar\Desktop>python a.py 
Application Version - 8.44.0 
Run on system - host 
User has admin rights - yes 
Start Time - 2016-03-07 13:44:55 
IP Address - 10.10.10.10 
CPU Count - 1 
System Type - Server 
System Uptime - 18.10 days 
Alarms - 0 
Warnings - 0 
Notices - 131 
Errors - 2 
Begin Time - 2016-03-07 13:44:55 
End Time - 2016-03-07 17:42:07 
+0

これは本当に動作します:-) Thanks Dinesh – Opera