I have data like the following:
id | src_ip | dst_ip | port |
----|----------------|--------------|------|
256 | 192.168.61.200 | 10.75.64.222 | 80 |
257 | 192.168.61.200 | 10.75.64.222 | 81 |
258 | 10.65.72.207 | 10.75.64.223 | 80 |
259 | 10.75.254.27 | 10.75.64.223 | 82 |
260 | 10.75.254.27 | 10.75.64.224 | 80 |
261 | 10.75.254.27 | 10.75.64.230 | 80 |
262 | 10.75.254.27 | 10.75.64.230 | 81 |
263 | 10.75.254.27 | 10.75.64.230 | 82 |
264 | 10.75.254.27 | 10.75.64.230 | 83 |
265 | 10.75.254.27 | 10.75.64.230 | 84 |
I built the schema and loaded the same data here: http://sqlfiddle.com/#!9/1185c
I'm planning to group the rows in a way to reproduce some rules for my firewall. Given the firewall supports ranges for ports or IPs I want to query this table and group as many rows as I can to produce valid rules.
There are a few different ways to group rows, but I want to start grouping by consecutive dst_ip
on the same port.
Given the table presented above, I would like to get something like this:
dst_ip | max_consecutive | port |
--------------|-----------------|------|
10.75.64.222 | 10.75.64.224 | 80 |
10.75.64.222 | 10.75.64.222 | 81 |
10.75.64.223 | 10.75.64.223 | 82 |
10.75.64.230 | 10.75.64.230 | 80 |
10.75.64.230 | 10.75.64.230 | 81 |
10.75.64.230 | 10.75.64.230 | 82 |
10.75.64.230 | 10.75.64.230 | 83 |
10.75.64.230 | 10.75.64.230 | 84 |
Note: max_consecutive
could be the number of consecutive rows, too, not necessarily the upper IP.
Actually, if I can go deeper, I would like to group by port
, too. That means the last 5 rows of the result will be summarized in just one like
10.75.64.230 | 10.75.64.230 | 80 | 84
I'm currently using MySQL, but I can move to PostgreSQL if it is necessary.
10.75.63.254, 10.75.63.255
continued with10.75.64.0
?