Use Cron to schedule enable and disable of an interface
-
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.
-
Determine the real name of your interface from Status -> Interfaces. For example, This tutorial will use an interface named 4GBACKUP with real name opt1.
-
Enable SSH in System -> Advanced -> Admin Access -> Enable Secure Shell (alternatively you can do the following steps from the console).
-
Connect with SSH (or console) with your admin login and select 8. Shell from the menu.
-
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.
-
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
- 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
- Press ESC to switch from insert mode to command mode, then type :wq to save the file.
- Repeat step 5 to create the disable interface file:
vi /root/disable-4GBACKUP.txt
- 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
- Repeat step 7 to save this file.
- 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).
- Issue the following command:
crontab -e
- 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
- Repeat step 7 to save the schedule.
- 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.
-
-
@micro8765 said in Use Cron to schedule enable and disable of an interface:
- 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. -
@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!
-
@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.
-
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.
-
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