Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Enable/disable existing rule via script

    Scheduled Pinned Locked Moved General pfSense Questions
    4 Posts 2 Posters 5.7k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      wge100
      last edited by

      Hello,

      I want to enable/disable an existing rule via a ssh script. Is this possible  ?

      William

      1 Reply Last reply Reply Quote 0
      • stephenw10S
        stephenw10 Netgate Administrator
        last edited by

        A firewall rule?
        This should be possible by calling the appropriate php script. Though I don't know what that is!  ;)
        I would experiment using the php shell until I knew what the config command is then find out how to execute that from a script.

        Unfortunately it looks like you have to know which rule it is by number. It's not something I have ever tried to do before but for example:

        pfSense shell: global $config;
        pfSense shell: parse_config(true);
        pfSense shell: print_r($config['filter']['rule']['3']);
        pfSense shell: exec
        Array
        (
            [id] =>
            [type] => pass
            [interface] => lan
            [tag] =>
            [tagged] =>
            [max] =>
            [max-src-nodes] =>
            [max-src-conn] =>
            [max-src-states] =>
            [statetimeout] =>
            [statetype] => keep state
            [os] =>
            [source] => Array
                (
                    [address] => 192.168.1.111
                )
        
            [destination] => Array
                (
                    [any] =>
                )
        
            [descr] => Loadbalancing Toshiba Laptop
            [gateway] => LoadBalance
            [disabled] =>
        )
        
        

        The above rule, rule 3, is disabled. Rules that are not disabled don't have that property in the config. You can then re-enable it like so:

        
        pfSense shell: global $config;
        pfSense shell: parse_config(true);
        pfSense shell: $config['filter']['rule']['3']['disabled'] = false;
        pfSense shell: write_config();
        pfSense shell: exec
        
        

        Or disable it again:

        
        pfSense shell: global $config;
        pfSense shell: parse_config(true);
        pfSense shell: $config['filter']['rule']['3']['disabled'] = true;
        pfSense shell: write_config();
        pfSense shell: exec
        
        

        If you use the recording feature you can make some php shell scripts out of those which you can then playback from any script. E.g.

        pfSsh.php playback enablerule3
        

        You may have to reload the firewall rules somewhere in there for it to take effect. I just made most of that up as I went along but it seems to work OK!  ;)

        Steve

        Edit: Hmm, looking at the example scripts in /etc/phpshellsessions it looks as though it maybe more complex than this. Maybe not!
        Also it looks like you would have to reload the firewall filters with:
        filter_configure_sync();

        1 Reply Last reply Reply Quote 0
        • W
          wge100
          last edited by

          hi, thank you for your information.

          i figured it out that it is firewall_rules.php?if=lan&act=toggle&id=10
          the id and code can you see in the status bar if you do a mouse over in the first column in the rule-list to toggle the rule.

          i miss now the appropriate php script to "apply changes". But i didn't find this one.
          status_filter_reload.php does not work. Any ideas ?

          i did a grep on filter_configure_sync, but to no avail.

          regards,
          William

          1 Reply Last reply Reply Quote 0
          • W
            wge100
            last edited by

            following code does the trick

            require_once("config.inc");
            require_once("functions.inc");
            require_once("filter.inc");
            require_once("shaper.inc");
            require_once("ipsec.inc");
            require_once("vpn.inc");
            
            /* invalidate interface cache */
            get_interface_arr(true);
            
            $retval = 0;
                            $retval = filter_configure();
            
                            clear_subsystem_dirty('filter');
            
                            pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/apply");
            
                            echo "The settings have been applied. The firewall rules are now reloading in the background.
            ";
            
            
            1 Reply Last reply Reply Quote 0
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • luckman212L luckman212 referenced this topic on
            • First post
              Last post
            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.