Python method for listing IPv4 vs IPv6 rules

Asked by Kevin Tate

Hello, all.

I have a question about listing out IPv4 vs IPv6 rules using Python. While working with the Python package PyUFW (https://pypi.org/project/pyufw/) I discovered that when listing the rules within UFW, it wasn't explicitly stating whether a rule was for IPv4 or IPv6 (see below).

1 allow 22/tcp
2 allow 80
3 allow 443
4 allow 22/tcp
5 allow 80
6 allow 443

After some investigation, it appears to me that the culprit for this behavior actually lies with the UFW source code. In the source code there is an object that stores IPv4 and IPv6 rules in separate lists. Those lists are then accessed with the following method:

def get_rules(self):
        '''Return list of all rules'''
        return self.rules + self.rules6

The issue with this is that the returned list doesn't contain an indication of what IP version each item corresponds to.

Is there a different method that allows me to distinguish between IPv4 and IPv6 rules? I could always access the object's variables directly (see example below), but I don't think that's best practice.

rules_ipv4 = backend.rules
rules_ipv6 = backend.rules6

Please let me know what you all think as well as if you have any questions. Thanks!

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu ufw Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
actionparsnip (andrew-woodhead666) said :
#1

I suggest you report a bug to highlight this issue.

Revision history for this message
Manfred Hampl (m-hampl) said :
#2
Revision history for this message
Kevin Tate (kevintate) said :
#3

I've gone ahead and created a bug for this (see linked). Thanks!

Can you help with this problem?

Provide an answer of your own, or ask Kevin Tate for more information if necessary.

To post a message you must log in.