java - How to parse IP addresses from Apache Server Log? -
i have find commonly occuring ip addresses apache logs.
12.1.12.1 9000 127.0.0.1 - frank [10/oct/2000:13:55:36 -0700] "get /apache_pb.gif http/1.0" 200 2326 "http://www.example.com/start.html" "mozilla/4.08 [en] (win98; ;nav)"
12.1.12.1 9000 192.145.1.23 - frank [10/oct/2000:13:55:36 -0700] "get /apache_pb.gif http/1.0" 200 2326 "http://www.example.com/start.html" "mozilla/4.08 [en] (win98; ;nav)"
how extract ip addresses (i.e. 3rd word in each line) using regular expressions in java? have find common ip addresses it, finding out robotic access. log contains millions of lines, regexp may suitable this.
here 1 solution:
string str1 = "12.1.12.1 9000 127.0.0.1 - frank [10/oct/2000:13:55:36" + " -0700] \"get /apache_pb.gif http/1.0\" 200 2326 " + "\"http://www.example.com/start.html\" \"mozilla/4.08 " + "[en] (win98; ;nav)\""; string str2 = "12.1.12.1 9000 192.145.1.23 - frank [10/oct/2000:13:55" + ":36 -0700] \"get /apache_pb.gif http/1.0\" 200 2326 " + "\"http://www.example.com/start.html\" \"mozilla/4.08 " + "[en] (win98; ;nav)\""; pattern p = pattern.compile("\\s+\\s+\\s+\\s+(\\s+).*"); matcher m = p.matcher(str1); if (m.matches()) system.out.println(m.group(1)); m = p.matcher(str2); if (m.matches()) system.out.println(m.group(1));
reg-ex breakdown:
\s+
, 1 or more non-white space characters.\s+
, 1 or more white space characters.- ...
(\\s+)
1 or more non-white space characters, captured in group 1.
Comments
Post a Comment