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

    Use Cron to schedule enable and disable of an interface

    Scheduled Pinned Locked Moved General pfSense Questions
    6 Posts 5 Posters 3.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.
    • M
      micro8765
      last edited by

      I had a requirement to disable an interface each evening and enable it again the next morning.

      After much searching and trial and error, I've made it work and thought I'd share my solution. I've seen a few people looking for something like this.

      1. Determine the real name of your interface from Status -> Interfaces. For example, This tutorial will use an interface named 4GBACKUP with real name opt1.

      2. Enable SSH in System -> Advanced -> Admin Access -> Enable Secure Shell (alternatively you can do the following steps from the console).

      3. Connect with SSH (or console) with your admin login and select 8. Shell from the menu.

      4. You'll be using the quirky linux vi editor for the next few steps. If you haven't used it before I'd recommend searching up a tutorial.

      5. Now we need to create a new file to contain the PHP Shell commands to enable the interface. Change the name to suit:

      vi /root/enable-4GBACKUP.txt
      
      1. Now you are in the editor, so press i for insert mode, then paste the following ensuring that you have replaced the real interface name from step 1:
      $config['interfaces']['opt1']['enable'] = "";
      write_config();
      exec
      exit
      
      1. Press ESC to switch from insert mode to command mode, then type :wq to save the file.
      2. Repeat step 5 to create the disable interface file:
      vi /root/disable-4GBACKUP.txt
      
      1. Repeat step 6 pasting this, once again ensuring you use the correct interface name from step 1:
      unset($config['interfaces']['opt1']['enable']);
      write_config();
      exec
      exit
      
      1. Repeat step 7 to save this file.
      2. Now we need to set-up cron to schedule the interfaces. The following instructions are an example that enables the interface at 7:30AM and disables it at 6:30PM, Monday through Friday (so it will be disabled nights & weekends).
      3. Issue the following command:
      crontab -e
      
      1. Repeat step 6 pasting the following. If you used different file names in steps 5 or 8, change the following lines to reflect this:
      30 7 * * 1-5 '/usr/local/sbin/pfSsh.php' < /root/enable-4GBACKUP.txt
      30 18 * * 1-5 '/usr/local/sbin/pfSsh.php' < /root/disable-4GBACKUP.txt
      
      1. Repeat step 7 to save the schedule.
      2. Type exit to quit back to the console menu. All done.

      That's all there is to it. I hope this helps someone out there.

      GrimsonG P 2 Replies Last reply Reply Quote 2
      • GrimsonG
        Grimson Banned @micro8765
        last edited by Grimson

        @micro8765 said in Use Cron to schedule enable and disable of an interface:

        1. Issue the following command:
        crontab -e
        

        Or simply use the cron package to edit/create cron jobs.

        Edit:
        You can also use the Filer package to easily add files, with the benefit of having all of it stored in the config for easy backup/restore.

        M 1 Reply Last reply Reply Quote 0
        • M
          micro8765 @Grimson
          last edited by

          @grimson said in Use Cron to schedule enable and disable of an interface:

          Or simply use the cron package to edit/create cron jobs.

          Edit:
          You can also use the Filer package to easily add files, with the benefit of having all of it stored in the config for easy backup/restore.

          Those are superb improvements - I have now implemented them. With those packages installed, no SSH or console is required. Many thanks!

          1 Reply Last reply Reply Quote 0
          • P
            pfpv @micro8765
            last edited by

            @micro8765, I believe you can do the same by simply using this in cron:

            /sbin/ifconfig igb0 down
            /sbin/ifconfig igb0 up
            

            Substitute "igb0" for your interface.

            1 Reply Last reply Reply Quote 0
            • AKEGECA
              AKEGEC
              last edited by

              Some people love to make things complex. The cron package is the simple way to do it as @Grimson suggested. Anyhow thanks for the input.

              1 Reply Last reply Reply Quote 0
              • U
                UClinux
                last edited by

                According to my test,

                unset($config['interfaces']['opt1']['enable']);
                interface_reconfigure('opt1');
                write_config('enable/disable opt1 interface');
                exec
                exit
                

                Real-time enable/disable interface

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.