gpt4 book ai didi

java - 过滤掉带有字符串字符的ip地址日志文件

转载 作者:行者123 更新时间:2023-11-30 06:41:23 27 4
gpt4 key购买 nike

这是我的 java 程序提取的日志文件的一部分,但我对这部分日志不感兴趣

2017-05-30 23:11:33,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:33,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication):
2017-05-30 23:11:33,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:32,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:31,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".
2017-05-30 23:11:18,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:18,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:15,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication):
2017-05-30 23:11:15,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626
2017-05-30 23:11:14,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:14,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:12,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:11,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:10,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]
2017-05-30 23:11:09,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: "rene1".



我只想要这些日志行中的 IP

2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]
2017-05-30 23:11:31,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]


我能想到的是使用正则表达式过滤掉这些行中的IP,只有一个IP对我来说就足够了,因为该IP地址属于最终用户IP地址。因此,我为正则表达式设置了自己的模式,但它没有按预期工作。以下是我的模式

(?<=accountName=rene1 remoteAddress=gateway/)[\\d.]+



This是我使用我的模式的测试网站,这里有人能够建议最合适的模式来过滤出我想要的结果吗?

最佳答案

你可以将这些日志消息放入列表中

并使用谓词进行过滤

示例:

List<String> myList = Arrays.asList("172.16.8.3",
"Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/255.16.8.3]",
"2017-05-30 23:11:33,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]",
"2017-05-30 23:11:33,682 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Logout agent success [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]",
"2017-05-30 23:11:33,819 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.KeyboardInteractiveAuthentication - SSH: Sent SSH_MSG_USERAUTH_INFO_REQUEST (Password Authentication): ",
"2017-05-30 23:11:33,871 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - User login attempt has been made from address /172.16.8.1:54626",
"2017-05-30 23:11:32,042 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]",
"2017-05-30 23:11:32,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]",
"2017-05-30 23:11:31,072 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking config agent [accountName=rene1 remoteAddress=gateway/172.16.8.1]",
"2017-05-30 23:11:31,090 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Config agent success [accountName=rene1 remoteAddress=gateway/172.16.8.1]",
"2017-05-30 23:11:31,091 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking auth agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]",
"2017-05-30 23:11:31,095 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - SSH: Failed login attempt on [172.16.8.1]. Username: \"rene1\".",
"2017-05-30 23:11:18,673 INFO [SSHD-TRANSFER-1] com.tumbleweed.st.server.sshd.AuthenticationProviderImpl - Invoking logout agent [accountName=rene1 remoteAddress=STEDGE/172.16.8.3]");

Predicate<String> predicate = t -> {
return t.indexOf("remoteAddress=gateway") != -1;
};
List<String> myFilteredList = myList.stream().filter(predicate).collect(Collectors.toList());
System.out.println(myFilteredList);
<小时/>

编辑:

要仅获取 IP 地址,请使用 UnaryOperator

UnaryOperator<String> operator = t -> {
String ipPatt = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";

Pattern pattern = Pattern.compile(ipPatt );
Matcher matcher = pattern.matcher(t);
if (matcher.find()) {
return matcher.group();
} else {
return "0.0.0.0";
}

};
List<String> myFilteredList = myList.stream().filter(predicate).collect(Collectors.toList());
myFilteredList.replaceAll(operator);

最终输出:

[172.16.8.1, 172.16.8.1, 172.16.8.1, 172.16.8.1]

关于java - 过滤掉带有字符串字符的ip地址日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44322961/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com