简体   繁体   English

用于隔离Apache访问日志文件中的Comcast IP地址的正则表达式

[英]Regular expression for isolating Comcast IP addresses in access log file for Apache

Really the fact I want to use this for my Apache access log file is arbitrary and irrelevant, but it gives context to the situation. 实际上,我想对我的Apache访问日志文件使用此事实是任意的和无关紧要的,但是它为情况提供了背景信息。

I need to filter out records associated with Comcast IP addresses. 我需要过滤出与Comcast IP地址关联的记录。 Here's a list of the dynamic IP address ranges that Comcast assigns. 这是Comcast分配的动态IP地址范围列表 I need a regular expression that can match all of those, and only those. 我需要一个可以匹配所有且仅那些匹配的正则表达式。 I'll work on it on my own in the mean time but I figured there would be some RegEx guru out there on SO that would enjoy the problem. 同时,我将自己进行处理,但是我认为在SO上会有一些RegEx专家可以解决这个问题。

Regex solution is possible, but very cumbersome, since the subnet mask is not multiple of 8. You will need to write a function to process the list and convert into regex. 正则表达式解决方案是可能的,但是非常麻烦,因为子网掩码不是8的倍数。您将需要编写一个函数来处理列表并将其转换为正则表达式。

It is better to use regex to grab the IP address and test the IP address against the list of IP addresses by Comcast. 最好使用regex来获取IP地址并通过Comcast对IP地址列表中的IP地址进行测试。 Simple implementation would be a set which allows you to search for the nearest number that is smaller than the argument. 一个简单的实现就是一个集合,它允许您搜索小于参数的最接近的数字。

That are a lot of IP adresses. 那是很多IP地址。

For example, 24.0.0.0/12 defines the IP range 24.0.0.1 - 24.15.255.255 . 例如, 24.0.0.0/12定义IP范围24.0.0.1 - 24.15.255.255 To match these numeric ranges with a regex: 要将这些数字范围与正则表达式匹配

24:    24
0-15:  [0-9]|1[0-5]
0-255: [0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]

Which gives 这使

(24)\.([0-9]|1[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])

And that's just for 24.0.0.0/12 , 293 to go. 那仅是24.0.0.0/12和293。

If you really want to do this you should write a small script to convert each IP range into a regex automatically. 如果您确实想这样做,则应编写一个小脚本,以将每个IP范围自动转换为正则表达式。

Another approach would be to match any IP address and feed it into a callback that does the matching using an appropriate module / framework / API. 另一种方法是匹配任何IP地址,并将其输入到使用适当的模块/框架/ API进行匹配的回调中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM