0

刚开始学java

我有以下数据集

Date first seen          Duration Proto      Source IP Addr:Port     Destination IP Addr:Port   Packets    Bytes Flows
2013-03-03 23:54:46.574     8.000 UDP      108.169.77.76:12345 ->    108.169.0.112:53           5      325     1
2013-03-03 23:59:51.984     0.000 UDP     100.253.69.196:62458 ->  100.256.234.129:1947         1       68     1
2013-03-03 23:59:52.048     0.000 UDP      108.450.45.35:123124 ->    108.123.0.987:9101        2     1686     1

我想用Java编写一个程序来只获取一列源IP地址和一列目标IP地址?有人可以告诉我如何做到这一点的示例代码吗?

4

3 回答 3

3

使用一个或多个空格作为分隔符来分割输入行会简单得多

String line =
    "2013-03-03 23:54:46.574     8.000 UDP      108.169.77.76:12345 ->    108.169.0.112:53           5      325     1";

String[] split = line.split(" +");
System.out.println(split[4]);
System.out.println(split[6]);

这输出:

108.169.77.76:12345
108.169.0.112:53

现在重用split方法,我们可以将 IP 地址与端口分开:

String[] split = line.split(" +");
System.out.println(split[4].split(":")[0]);
System.out.println(split[6].split(":")[0]);

这将输出不带端口的 IP 地址:

108.169.77.76
108.169.0.112

请注意,此解决方案缺少检查,以防 ArrayIndexOutOfBoundsException万一您的数据看起来不像您预期​​的那样出现异常。在使用运算符之前,您必须始终验证数组中元素的数量[..]

更新: IPv6 地址

如果 IPv6 地址是可能的,您可以使用:

String[] split = line.split(" +");
System.out.println(getIP(split[4]));
System.out.println(getIP(split[6]));

private static String getIP(String ipWithPort) {
    int index = ipWithPort.lastIndexOf(":");
    if (index < 0) {
        throw new RuntimeException("Invalid format: " + ipWithPort);
    }
    return ipWithPort.substring(0, index);
}
于 2013-05-17T09:51:07.430 回答
2

只需添加到@BorisBrodski 答案:

文本文件应按行拆分。在 Java 中,您可以使用BufferedReader.

try {
    File file = new File("/where/my/file/is.txt");
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line = null;
    while ((line = in.readLine()) != null) {
        // ... do something with `line`
    }
} catch (FileNotFoundException e) {
    // ... handle exception
} catch (IOException e) {
    // ... handle exception
}
于 2013-05-17T09:44:46.973 回答
1

假设您逐行读取带有输入数据的文本文件。然后您可以使用此代码段解析每一行并获取源和目标 IP 地址:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    private static final String REGEX_IP = "(?:[0-9]{1,3}\\.){3}[0-9]{1,3}";
    private static final Pattern LINE_PATTERN = Pattern.compile(
        "(" + REGEX_IP + "):[0-9]+\\s+->\\s+(" + REGEX_IP + "):[0-9]+");

    public static void main(final String[] args) {
        String line =
            "2013-03-03 23:54:46.574     8.000 UDP      108.169.77.76:12345 ->    108.169.0.112:53           5      325     1";

        Matcher matcher = LINE_PATTERN.matcher(line);
        if (matcher.find()) {
            System.out.println("Source IP: " + matcher.group(1));
            System.out.println("Destination IP: " + matcher.group(2));
        }
        else {
            System.out.println("No match");
        }
    }
}

正则表达式匹配器解析行(find()方法),如果找到匹配,则提供匹配组中的两个 IP 地址(...)

  • matcher.group(1)- 源 IP
  • matcher.group(2)- 目标 IP
于 2013-05-17T09:42:17.377 回答