0

I am trying to filter out private network IPfrom URLs.
Previously I had this code

 if(!sCurrentLine.startsWith("192.168") && !sCurrentLine.startsWith("172.") && !sCurrentLine.startsWith("10.")&& !sCurrentLine.startsWith("127.0.0"))

Data Example

10.1.1.83/nagiosxi/
10.1.1.83/nagiosxi//rr.php?uid=18-c96b5fb53f9
127.0.0.1/tkb/internet/
172.18.20.200/cgi-bin/topstats.pl?submit_type=topstats&time=00:2

Here is my new code

 Pattern privateIp=Pattern.compile("(^127\\.0\\.0\\.1)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)");
 while((sCurrentLine=br.readLine())!=null)
{

            Matcher pnm=privateIp.matcher(sCurrentLine);

            if(!pnm.matches())
            {
              System.out.println("not match");

            }
}

I am thinking of using Pattern Match. However, Pattern.compile doesn't take this regular expression.

Or is there any function that can handle private network IP.

Any thought on that?

4

3 回答 3

2

这只是一个猜测,但您是否可能没有\在表示模式的字符串中转义,因为

Pattern.compile("(^127\\.0\\.0\\.1)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)");

对我来说编译得很好(尽管我还没有在任何数据上测试它)。

修改后更新

我可以看到您正在使用matches方法来检查您的正则表达式是否位于行首。这不起作用,因为matches检查正则表达式是否匹配整个数据。尝试将此方法更改为find

if (!pnm.find()) {
    System.out.println("not match");
}
于 2013-08-23T00:39:57.690 回答
1

使用 InetAddress 类:

public static void main(String[] args) {
    String[] addrs = {"127.0.0.1", "8.8.8.8", "172.1.2.3", "10.200.34.5", "192.168.111.1", "fc00::1", "www.google.com"};
    for (String a : addrs) {
        try {
            InetAddress ina = InetAddress.getByName(a);
            System.out.printf("Is %s private? %s\n", ina.toString(), ina.isSiteLocalAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

产生:

Is /127.0.0.1 private? false
Is /8.8.8.8 private? false
Is /172.1.2.3 private? false
Is /10.200.34.5 private? true
Is /192.168.0.1 private? true
Is /fc00:0:0:0:0:0:0:1 private? false
Is www.google.com/173.194.46.20 private? false
于 2013-08-23T01:39:31.550 回答
0

You can use following code to find out if the IPAddress is Local or external. This checks for RFC 1918

String ipAddressAr[]    =   
            { "192.168.1.1" , 
              "172.18.20.200",
              "10.1.1.83",
              "127.0.0.1",
              "172.18.20.200"


            };


        String ipAddress    =   null;
        String ipAddressSplit[] =   null;
        int ipAddressIntSplit[] =   {0,0,0,0};

        for (int i = 0; i < ipAddressAr.length; i++) {
            ipAddress = ipAddressAr[i];
            ipAddressSplit = ipAddress.split("\\.");
            ipAddressIntSplit[0]    = Integer.parseInt ( ipAddressSplit[0] );
            ipAddressIntSplit[1]    = Integer.parseInt ( ipAddressSplit[1] );
            ipAddressIntSplit[2]    = Integer.parseInt ( ipAddressSplit[2] );
            ipAddressIntSplit[3]    = Integer.parseInt ( ipAddressSplit[3] );

            /*
             * RFC 1918
             * 
            The Internet Assigned Numbers Authority (IANA) has reserved the
               following three blocks of the IP address space for private internets:

                 10.0.0.0        -   10.255.255.255  (10/8 prefix)
                 172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
                 192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
            */ 
            if ( 
                    ( ipAddressIntSplit[0] == 10 ) ||
                    ( ( ipAddressIntSplit[0] == 172 ) && ( ipAddressIntSplit[1] >= 16 && ipAddressIntSplit[1] <= 31 ) ) ||
                    ( ipAddressIntSplit[0] ==   192  && ipAddressIntSplit[1] == 168  ) ||
                    ( ipAddressIntSplit[0] ==   127  && ipAddressIntSplit[1] == 0 && ipAddressIntSplit[2] == 0  ) 
                    ){
                System.out.println( "IP Address: " + ipAddress + " is Local. ");
            }else{
                System.out.println( "IP Address: " + ipAddress + " is NOT Local. ");
            }
        }
于 2013-08-23T02:24:19.193 回答