从ifconfig获取inet,bcast和mask

时间:2014-06-04 09:40:52

标签: java regex

我试图从java中的ifconfig命令获取任何和所有inet,bcast和掩码IP。但我正在努力让正则表达式正确。

这是我天真的尝试:我尝试了几种使用“\ s”和“”作为空格的变体。

String string;
try {
    // discover ip addresses
    writer.write("Discovering available network connections...\n\n");
    System.out.println("-------------------------");
    // String ifconfigCmd = "/sbin/ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'";
    String ifconfigCmd = "ifconfig";
    System.out.println("executing `" + ifconfigCmd + "`");

    Process ifconfigProc = Runtime.getRuntime().exec(ifconfigCmd);
    BufferedReader ifconfigProcReader = new BufferedReader(new InputStreamReader(ifconfigProc.getInputStream()));

    // read output of ifconfig command
    HashSet<String> subnets = new HashSet<>();
    String ipaddress, inet, bcast, mask;
    String ipaddressPattern = "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                               "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                               "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                               "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
    while ((ipaddress = ifconfigProcReader.readLine()) != null) {
        System.out.println("ifconfig       : " + ipaddress);

        String pStr = ".*(inet addr:(?<inet>" + ipaddressPattern + "))?.*(Bcast:(?<bcast>" + ipaddressPattern + "))?.*(Mask:(?<mask>" + ipaddressPattern + "))?.*";
        Matcher m = Pattern.compile(pStr).matcher(ipaddress);
        if (m.matches()) {
            try {
                inet = m.group("inet");
                System.out.println("inet           : " + inet);
                if (inet != null) {
                    int i = inet.lastIndexOf(".");
                    System.out.println("last index of .: " + i);
                    if (i >= 0) {
                        ipaddress = ipaddress.substring(0, i+1) + "*";
                        System.out.println("subnet         : " + ipaddress);
                        subnets.add(ipaddress);
                    }
                }
            }
            catch (IllegalStateException e) {}

            try {
                bcast = m.group("bcast");
                System.out.println("bcast          : " + bcast);
            }
            catch (IllegalStateException e) {}

            try {
                mask = m.group("mask");
                System.out.println("mask           : " + mask);
            }
            catch (IllegalStateException e) {}
        }
    }

有人能帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:2)

这是我要做的一种方式:

    ....
    inet = null;
    bcast = null;
    mask = null;
    ....
    while ((ipaddress = ifconfigProcReader.readLine()) != null) {
            if (inet != null && bcast != null && mask != null)
            {
                break;
            }
            if (inet == null)
            {
                inet = search("inet addr:[0-9.]+", ipaddress, "inet addr:");
            }
            if (bcast == null)
            {
                bcast = search("Bcast:[0-9.]+", ipaddress, "Bcast:");
            }
            if (mask == null)
            {
                mask = search("Mask:[0-9.]+", ipaddress, "Mask:");
            }
        }
    //check if you have got all the ip address populated
    // in inet, bcast, mask
    ....
    ....


        private String search(String regex, String line, String removeString)
        {
            Pattern compiledPattern = Pattern.compile(regex);
            Matcher matcher = compiledPattern.matcher(line);
            String ipAddress = null;
            if (matcher.find())
            {
                ipAddress = matcher.group().replaceFirst(removeString, "");
            }
            return ipAddress;
        }

你也可以像这样使用正则表达式:

....
regex = "(inet addr:)([0-9.]+)";
.....

if(matcher.find()){
    ipAddress = matcher.group(2);
}