2017-05-30 3 views
0

Mein Programm gibt einen grep Befehl aus, um den Protokollbasis-Zeitbereich und ein eindeutiges Schlüsselwort zu durchsuchen. Mein Programm in der Lage, um den grep Befehl erfolgreich ausgestellt und es ist mehrere abgestimmte Linie von Log zurück, die wie folgt aussehen

Mustererkennung für Protokollanalyse

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 



Aber ich all dies nicht benötigen, die Dinge, die ich interessiert [remoteAddress=/172.16.8.1:64931]. Diese Codezeile Pattern pat1 = Pattern.compile("remoteAddress=/(\d)"); gibt das illegale Escapezeichen. Kann ich wissen, wie man die IP-Adresse nur ohne irgendeine Portnummer extrahiert und sie in einer Zeichenkettenvariablen speichert, ich hatte einige Informationen auf Google gesucht, aber es funktioniert nicht? Für Sie Hinweis, dies ist mein Quellcode

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

Ich mag nicht, wie Sie "Argument" buchstabieren –

Antwort

1

Diese Regex entspricht Ziffern und Punkten nach remoteAddress=/ Phrase.

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); 
     } 

    } 

Es passt nicht remoteAddress=STEDGE/172.16.8.3.

Es nutzt positiven Lookbehind zu behaupten, dass (?<=remoteAddress=/) ist vor 172.16.8.1

Muster:

(?<=remoteAddress=/) positiven Lookbehind (Länge Null Behauptung). Es passt nur, wenn [\\d.]+ der genaue Ausdruck remoteAddress=/ vorausgeht.

[\\d.]+ Ziffer oder Periode übereinstimmen. für 1 oder mehrere Male. Passt nicht zu etwas anderem.

+0

Vielen Dank, es funktioniert wie erwartet, aber können Sie mir helfen, das Muster zu verstehen, das Sie eingestellt hatten? – attack

0

Versuchen cut Befehl nach grep. Etwas wie:

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

Siehe https://shapeshed.com/unix-cut/

+1

Danke für Ihren Führer auch. lernte auch neue Dinge – attack