Last active
December 20, 2023 19:41
-
-
Save evindunn/7b960e20e113a00425020d69a9dcc1c9 to your computer and use it in GitHub Desktop.
Poll Firewalld State as JSON
This file contains hidden or 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 | |
import dbus | |
import json | |
FIREWALLD_BUS = "org.fedoraproject.FirewallD1" | |
FIREWALLD_RUNTIME_OBJ = "/org/fedoraproject/FirewallD1" | |
FIREWALLD_CONFIG_OBJ = "/org/fedoraproject/FirewallD1/config" | |
def main(): | |
system_bus = dbus.SystemBus() | |
firewalld_runtime = system_bus.get_object( | |
FIREWALLD_BUS, | |
FIREWALLD_RUNTIME_OBJ | |
) | |
firewalld_config = system_bus.get_object( | |
FIREWALLD_BUS, | |
FIREWALLD_CONFIG_OBJ | |
) | |
default_zone = firewalld_runtime.getDefaultZone() | |
zones = {str(zn): {} for zn in list(firewalld_config.getZoneNames())} | |
for zone in zones.keys(): | |
zone_obj_path = firewalld_config.getZoneByName(zone) | |
zone_obj = system_bus.get_object( | |
FIREWALLD_BUS, | |
zone_obj_path | |
) | |
zone_settings = zone_obj.getSettings2() | |
zones[zone] = zone_settings | |
print( | |
json.dumps({ | |
"default_zone": default_zone, | |
"zones": zones | |
}) | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment