Pulling off IP address range filtering with Regex in AWStats

regex ip address range

regex ip address rangeRecently I was tasked to create an extra section in an exisiting AWStats configuration which would basically filter users” IP addresses that are contained in a specific range.

In order to do this, I needed to create a new ExtraSection for the administrator to be able to see this list inside the same report used for the website, but in a different section of the report output. ExtraSections in AWStats is a handy feature which enables the creation of a custom report concerning data for specific parts of a website”s traffic.

In order to create the ExtraSection in the first place, one should read up on the official documentation of AWStats, specifically AWStats docs covers the subject extensively.

So, with all the introductory stuff out of the way, onto the task at hand. The problem was defined as follows: “Find all traffic concerning 2 different groups of visitor addresses. Visitors with a DNS ending in example.com and visitors with IP addresses in the range of 172.16.90.* to 172.16.223.*” (the addresses used here are random).

The first part seems easy, and sure enough it is. All that’s needed is an application of the rule “any string ending in example.com” to catch the correct host addresses. Converting the above to REGEX speak, it would be

[color-box]^(.*example\.com)$[/color-box]

The second part is a bit trickier. What’s needed here is a step by step approach, which makes the whole task less daunting than it might initially seem. Knowing that the first two parts of the IP address are static the regular expression can start taking its form:

[color-box]^(172.\16\.(.*))$[/color-box]

What the above line says is “Catch all strings starting with 172.16 and ending in whatever”. The tricky part is to actually get the ranges needed. In order for that to work, we need to use conditional statements inside the regex, specifically OR clauses (indicated by | ).

One step at a time, the process is as follows:

First to get everything from .90 to .99
[color-box]^(172\.16\.(9[0-9])\.(.*))$[/color-box]

To get everything from 100 to 199
[color-box]^(172\.16\.(1[0-9][0-9])\.(.*))$[/color-box]

To get everything from 200 to 223
[color-box]^(172\.16\.(2[0-1][0-9]|22[0-3])\.(.*))$[/color-box]

Lastly, to get the whole range, the regex clause will be
[color-box]^(172\.16\.(9[0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\.(.*))$[/color-box]

Finished with the regex clause, the only thing left to do is to build the AWStats ExtraSection itself. One thing to note here is that while the OR statement in the above clause was represented with a ”|”, as far as AWStats clauses are concerned, OR is written ”||”. Below is the rule used in the AWStats configuration file itself:

[color-box]ExtraSectionName1=”Specific Visits” [/color-box]
[color-box]ExtraSectionCodeFilter1=”200 304″[/color-box]
[color-box]ExtraSectionCondition1=”HOST,^(.*example\.com)$ || HOST,^(172\.16\.(9[0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\.(.*))$”[/color-box]
[color-box]ExtraSectionFirstColumnTitle1=”Visitor”[/color-box]
[color-box]ExtraSectionFirstColumnValues1=”HOST,^(.*)$”[/color-box]
[color-box]ExtraSectionFirstColumnFormat1=”%s”[/color-box]
[color-box]ExtraSectionStatTypes1=PHBL[/color-box]
[color-box]ExtraSectionAddSumRow1=1[/color-box]
[color-box]MaxNbOfExtra1=1000[/color-box]
[color-box]MinHitExtra1=1[/color-box]

Some resources that I’ve found to be really useful while looking around the web for answers about AWStats in general:
[1] Official AWStats documentation
[2] IO AWStats forum

Read More Post