Skip to content

Instantly share code, notes, and snippets.

@EcmaXp
Created January 21, 2024 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EcmaXp/16d844c24a167be355ee9ea4fd257646 to your computer and use it in GitHub Desktop.
Save EcmaXp/16d844c24a167be355ee9ea4fd257646 to your computer and use it in GitHub Desktop.
Export UFW (Uncomplicated Firewall) rules as UFW commands using the iptables backend
#!/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