[英]Regarding private network IP filtering
I am trying to filter out private network IPfrom URLs. 我正在尝试从URL过滤出专用网络IP。
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. 但是,
Pattern.compile
不采用此正则表达式。
Or is there any function that can handle private network IP. 或者是否有任何功能可以处理专用网络IP。
Any thought on that? 有什么想法吗?
It is just a guess but is it possible that you are not escaping \\
in String representing pattern because 只是一个猜测,但是有可能您没有在表示模式的字符串中转义
\\
Pattern.compile("(^127\\.0\\.0\\.1)|(^10\\.)|(^172\\.1[6-9]\\.)|(^172\\.2[0-9]\\.)|(^172\\.3[0-1]\\.)|(^192\\.168\\.)");
compiles fine for me (although I didn't test it on any data yet). 编译对我来说很好(尽管我尚未在任何数据上对其进行测试)。
I can see that you are using matches
method to check if you regex is at start of line. 我可以看到您正在使用
matches
方法检查正则表达式是否在行首。 This wont work because matches
checks if regex matches entire data. 这将无法正常工作,因为
matches
检查正则表达式是否匹配整个数据。 Try changing this method to find
尝试更改此方法以
find
if (!pnm.find()) {
System.out.println("not match");
}
Use the InetAddress class: 使用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();
}
}
}
produces: 产生:
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
You can use following code to find out if the IPAddress is Local or external. 您可以使用以下代码来查找IPAddress是本地还是外部。 This checks for RFC 1918
这检查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. ");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.