2017-05-30 17 views
0

私のプログラムは、時間範囲とユニークなキーワードでログベースを検索するコマンドgrepを発行します。できる私のプログラムが正常にgrepコマンドを発行し、それが次

ログ解析のパターンマッチング

22:41.9 INFO SSHD SSHD-TRANSFER-1 [accountName=root] [remoteAddress=/172.16.8.1:64931]:Logout agent success [accountName=null remoteAddress=STEDGE/172.16.8.3] AuthenticationProviderImpl.java com.tumbleweed.st.server.sshd.AuthenticationProviderImpl executeLogoutAgent 429 UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN 
21:45.9 INFO SSHD SSHD-TRANSFER-1 [accountName=root] [remoteAddress=/172.16.8.1:64931]:Invoking logout agent [accountName=null remoteAddress=STEDGE/172.16.8.3] AuthenticationProviderImpl.java com.tumbleweed.st.server.sshd.AuthenticationProviderImpl executeLogoutAgent 425 UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN 
21:45.9 INFO SSHD SSHD-TRANSFER-1 [accountName=root] [remoteAddress=/172.16.8.1:64931]:SSH: User "null" logged out from [172.16.8.1]. AuthenticationProviderImpl.java com.tumbleweed.st.server.sshd.AuthenticationProviderImpl executeLogoutAgent 422 UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN 



のように見えるログのいくつかのマッチした行を返すのですが、私はこのすべてを必要としない、物事私は興味があります[remoteAddress=/172.16.8.1:64931]。このコード行Pattern pat1 = Pattern.compile("remoteAddress=/(\d)");は、不正なエスケープ文字を与えています。私はポート番号を使わずにIPアドレスを抽出して文字列変数に格納する方法を知っていて、Googleでいくつかの情報を検索しましたが機能しません。あなたが参考のために、これは私のソースコード

import java.io.*; 
import java.util.regex.*; 
class blockIP 
{ 
    public static void main(String [] args) 
    { 
    String command1 = "date +%R"; 
    String time = null; 
    String arguement2 = null; 
    String arguement1 = ".*java"; 
    try 
     { 
      Process p1 = Runtime.getRuntime().exec(command1); 
      BufferedReader br1 = new BufferedReader(new InputStreamReader(p1.getInputStream())); 

      String line1; 
      while((line1 = br1.readLine()) != null) 
       { 
        System.out.println(line1); 
        time = line1; 
        arguement2 =time.concat(arguement1); 
       } 
      br1.close(); 
      String command2 = "grep "+arguement2+" stlog.txt"; 
      System.out.println("the command2 is :"+command2); 
      Process p2 = Runtime.getRuntime().exec(command2); 
      BufferedReader br2 = new BufferedReader(new InputStreamReader(p2.getInputStream())); 
      String line2; 
      while((line2 = br2.readLine()) != null) 
      { 
       System.out.println(line2); 
       Pattern pat1 = Pattern.compile("remoteAddress=/(\d)"); 
       Matcher matcher1 = pat1.matcher(line2); 
       while(matcher1.find()) 
        { 
        System.out.println(matcher1.group(1)); 
        } 
      } 

     } 
     catch(IOException e) 
     { 
     e.printStackTrace(); 
     } 

    } 
} 
+0

私はあなたが "引数"を綴る方法が好きではない –

答えて

1

を参照してください。

public static void main(String[] args) { 
     String s = "21:45.9 INFO SSHD SSHD-TRANSFER-1 [accountName=root] [remoteAddress=/172.16.8.1:64931]:Invoking logout agent [accountName=null remoteAddress=STEDGE/172.16.8.3] AuthenticationProviderImpl.java com.tumbleweed.st.server.sshd.AuthenticationProviderImpl executeLogoutAgent 425 UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN\r\n"; 
     Pattern pattern = Pattern.compile("(?<=remoteAddress=/)[\\d.]+"); 
     Matcher matcher = pattern.matcher(s); 
     while (matcher.find()) { 
      String group = matcher.group(); 
      System.out.println(group); 
     } 

    } 

remoteAddress=STEDGE/172.16.8.3と一致しません。

それ(?<=remoteAddress=/)であることを主張する正の後読みを使用

172.16.8.1前パターン:

(?<=remoteAddress=/)正後読み(長さゼロの主張)。 [\\d.]+の前に完全なフレーズremoteAddress=/がある場合にのみ一致します。

[\\d.]+一致する数字またはピリオド。 1回以上。他のものとは一致しません。

+0

ありがとう、それは期待どおりの作品ですが、あなたが私が設定したパターンを理解するのを助けることができますか? – attack

0

grepをした後、カットコマンドを使用してみてくださいです。ような何か:

grep argument stlog.txt | cut -d ' ' -f 6 

は、この正規表現はremoteAddress=/フレーズの後に数字とドットと一致するhttps://shapeshed.com/unix-cut/

+1

あなたのガイドもありがとう。 – attack