Java 关于专用网络IP过滤

Java 关于专用网络IP过滤,java,Java,我正在尝试从URL中筛选出专用网络IP地址。 以前我有这个代码 if(!sCurrentLine.startsWith("192.168") && !sCurrentLine.startsWith("172.") && !sCurrentLine.startsWith("10.")&& !sCurrentLine.startsWith("127.0.0")) 数据示例 10.1.1.83/nagiosxi/ 10.1.1.83/nagiosxi

我正在尝试从URL中筛选出专用网络IP地址。
以前我有这个代码

 if(!sCurrentLine.startsWith("192.168") && !sCurrentLine.startsWith("172.") && !sCurrentLine.startsWith("10.")&& !sCurrentLine.startsWith("127.0.0"))
数据示例

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
这是我的新代码

 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");

            }
}
我正在考虑使用模式匹配。但是,
Pattern.compile
不接受此正则表达式

或者是否有任何功能可以处理专用网络IP


对此有何想法?

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

Pattern.compile("(^127\\.0\\.0\\.1)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)");
编译对我来说很好(尽管我还没有对任何数据进行测试)

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

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

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

Pattern.compile("(^127\\.0\\.0\\.1)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)");
编译对我来说很好(尽管我还没有对任何数据进行测试)

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

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

使用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

使用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

您可以使用以下代码确定IP地址是本地的还是外部的。这将检查RFC1918

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. ");
            }
        }
String ipAddressAr[]=
{ "192.168.1.1" , 
"172.18.20.200",
"10.1.1.83",
"127.0.0.1",
"172.18.20.200"
};
字符串ipAddress=null;
字符串ipAddressSplit[]=null;
int-ipAddressIntSplit[]={0,0,0};
for(int i=0;i((IPAddressInSplit[0]==172)和&&(IPAddressInSplit[1]>=16&&IPAddressInSplit[1]您可以使用以下代码来确定IPAddress是本地的还是外部的。这将检查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. ");
            }
        }
String ipAddressAr[]=
{ "192.168.1.1" , 
"172.18.20.200",
"10.1.1.83",
"127.0.0.1",
"172.18.20.200"
};
字符串ipAddress=null;
字符串ipAddressSplit[]=null;
int-ipAddressIntSplit[]={0,0,0};
for(int i=0;i((IPAddressInSplit[0]==172)和&(IPAddressInSplit[1]>=16&&IPAddressInSplit[1]
模式。编译时不使用此正则表达式
能否向我们显示您的代码和错误信息/堆栈跟踪(如果有)?为什么需要正则表达式?请注意“^172”对于classB类型中的私有IP来说不够好,可以是172.16.xxx.yyy到172.31.xxx.yyy之间的任何内容。例如,172.0.0.1是公共的,而不是私有的(并且属于172-0-0-1.lightspeed.brhmal.sbcglobal.net)
模式。编译时不使用此正则表达式
能否向我们展示您的代码和错误信息/堆栈跟踪(如果有的话)?为什么需要正则表达式?请注意,“^172.”对于classB类型中的私有IP不够好,可以是172.16.xxx.yyy到172.31.xxx.yyy之间的任何内容。也就是说,172.0.0.1是公共的,而不是私有的(并且属于172-0-0-1.lightspeed.brhmal.sbcglobal.net)@德拉福德,正如我所说,这只是猜测,因为没有你的实际代码,我将无法为你提供更多帮助。@德拉福德,你能添加你用来测试它的数据示例吗?当我输入
“127.0.0.1”
sCurrentLine
中,它似乎工作正常。请检查它。我想我应该拆分一个域并执行此操作match@draford
matches()
检查整个数据是否与regex匹配,在IP之后的情况下,有些数据与regex不匹配。请尝试将方法更改为
find()
相反。@draford,正如我所说的,那只是猜测,因为没有你的实际代码,我将无法为你提供更多帮助。@draford,你能添加你用来测试它的数据示例吗?当我把
“127.0.0.1”
放在中时