Created
January 21, 2024 13:19
-
-
Save EcmaXp/16d844c24a167be355ee9ea4fd257646 to your computer and use it in GitHub Desktop.
Export UFW (Uncomplicated Firewall) rules as UFW commands using the iptables backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
/usr/local/bin/ufw-export | |
""" | |
import gettext | |
from ufw.backend_iptables import UFWBackendIptables # noqa | |
from ufw.parser import UFWCommandRule, UFWCommandRouteRule # noqa | |
def main(): | |
gettext.install("ufw") | |
rules = [] | |
for rule in UFWBackendIptables(dryrun=True).get_rules(): | |
if not rule.forward: | |
rules.append(("ufw", UFWCommandRule.get_command(rule))) | |
else: | |
rules.append(("ufw", "route", UFWCommandRouteRule.get_command(rule))) | |
visited_rules = set() | |
for rule in rules: | |
if rule not in visited_rules: | |
visited_rules.add(rule) | |
print(*rule) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment