[Solved]Activate / deactivate WAN with timetable / Schedule

Started by hirschferkel, September 14, 2020, 05:06:51 PM

Previous topic - Next topic
September 14, 2020, 05:06:51 PM Last Edit: September 18, 2020, 08:17:06 PM by hirschferkel
Is there any way to activate a WAN (Wifi) and to switch it of e.g. during weekends to save energie? I was able to create a schedule, but I can only find ways to administrate services with the firewall but no way to ton on / turn of a Wifi?

Best, hirschferkel

have you thought about writing a simple script with ifconfig <WAN interface name> down and another one for up and add it to your CRON jobs?

Hmm I would... if I could...
Absolutely no idea how to write such a script and how to apply it. Anny help would be really appreciated. Sounds like it would work, but as I mentioned, I have no idea how to create it...

Quote from: littlepepper on September 15, 2020, 01:41:52 PM
have you thought about writing a simple script with ifconfig <WAN interface name> down and another one for up and add it to your CRON jobs?

September 16, 2020, 05:22:45 PM #3 Last Edit: September 16, 2020, 06:01:22 PM by littlepepper
In general brush you would need to ssh into your opnsense machine.

You would need to write 2 text files (scripts) one for up and one for down using a text editor like vi. It can be as simple as

#!/bin/sh
ifconfig <your interface here> up

then add the scripts to cron (of course you need to figure out the when)

Here is a reference: https://docs.opnsense.org/development/backend/configd.html

If you are not comfortable, it is best to buy some support hours and get it looked at instead

https://www.deciso.com/business-support/

September 16, 2020, 11:08:42 PM #4 Last Edit: September 16, 2020, 11:11:21 PM by hirschferkel
Seems practicable, but where do I need to arrange the "when". Within the scripts or do I have to schedule the cron-jobs? Actually I found the reference, but it's one of those no one will ever understand. It's written for those who know how it works and don't need a description... those who need one, won't understand it.

Is it an external Wifi access point?
OPNsense 25.7a - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

No it's an OPNsense Access Point so basically the idea is right, to switch on/off a ath0 interface temporarily.


OK. it's pretty simple, I'll try and guide you through it.


First thing you need as has been pointed out is a couple of scripts, one to enable, one to disable, this can actually be done in one script and we'll use that method. We need to call the function ifconfig igb0 up or ifconfig igb0 down, I've attached the script that calls these functions. You may need to change it as the interface may not be igb0.


Next you need to create the conf file that configd uses to add the functions to the Cron avalable functions list. I've created that too and attached it.


So you have the scripts and we need to put them in the correct directories. Copy the *.sh file to the folder /usr/local/etc/rc.d, once copied set its permissions to 755 so it's executable. Next copy the *.conf file to the /usr/local/opnsense/service/conf/actions.d folder. Now we need to tell opnsense to read the new config file so that the calls are added to the cron functions list. In a shell, enter the following command:
# service configd restart

Finally, we need to add the Cron events themselves, so go to System: Settings: Cron and click on the '+' to add a new event. Set the day, time etc of the interface down event and the select the 'Disable Interface' from the command list. Repeat for the up event and select the 'Enable Interface'.


That's it, it should all work. Try and understand what's happening rather than just copy and paste, it will prove beneficial down the line if  you understand what's going on.

OPNsense 25.7a - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

Oh great, I'll try to set it up next days and give a feedback on it!
Thanks a lot in advance.

Best, hirschferkel

September 18, 2020, 11:04:47 AM #9 Last Edit: September 18, 2020, 02:18:42 PM by hirschferkel
@marjohn56
I replaced the interface igb0 in the script with ath0_wlan2
e.g. ifconfig ath0_wlan2 down

Than I tried to set it up and it took me a while, as I had to enable ssh. (Did not recognize that it was not activated a while, as I was able to login ...but as I saw later connection was closed immediately afterwards)
I had to enable root, too, with ssh (Copying files did not work with admin users, which I would not have expected to fail)
I had to move the files from my home to the ssh server... was tricky to find the right way to copy from home to ssh server.
Finally I finalized all steps and created a test cron job to see if it works

   0   11   18   09   5   Test Disable   Disable Interface   
   5   11   18   09   5   Test Enable   Enable Interface

and --- it did not...
So time is 11.03 Friday 18th of September and Interface is up...

I tried a different setting:

   14   11   *   *   *   Test Disable   Disable Interface   
   15   11   *   *   *   Test Enable   Enable Interface

...11:14:30... ath0_wlan2 Interface is still up.
Doesn't work so far, too.

Rebootet.
Applied the "button"... no result. Any idea?

September 18, 2020, 02:18:07 PM #10 Last Edit: September 18, 2020, 02:19:41 PM by hirschferkel
So the cron job is being executed, but aborted with returned exit status 127?

Quote from: marjohn56 on September 17, 2020, 10:06:59 AM
That's it, it should all work. Try and understand what's happening rather than just copy and paste, it will prove beneficial down the line if  you understand what's going on.

So i found these log entries:
2020-09-18T11:50:00 configd.py[32149] [83286599-3f93-4a5c-895d-149fcb25ad83] returned exit status 127
2020-09-18T11:50:00 configd.py[32149] [83286599-3f93-4a5c-895d-149fcb25ad83] Disabling Interface


tried to execute another test:
2020-09-18T14:14:32 configd.py[32149] [37f6a30b-8b51-41f0-8de3-fd7f528db74f] restarting cron
2020-09-18T14:14:32 configd.py[32149] OPNsense/Cron generated //var/cron/tabs/nobody
2020-09-18T14:14:32 configd.py[32149] generate template container OPNsense/Cron


same again:
2020-09-18T14:16:00 configd.py[32149] [9945d40d-1b08-47c1-b793-200f0cac8655] returned exit status 127
2020-09-18T14:16:00 configd.py[32149] [9945d40d-1b08-47c1-b793-200f0cac8655] Disabling Interface

Let me check locally, see if I can see what the issue is.
OPNsense 25.7a - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

Ok, must have been having a 'doh' moment... replace the conf file with the one attached as there was an error. However, the error you are seeing would suggest you haven't set the executable flags on the sh file, so check that.
OPNsense 25.7a - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

You can test from the shell to see if it works, type the following in a shell.


To bring the interface up
#  /usr/local/etc/rc.d/interface_up_down.sh up
To take it down
#  /usr/local/etc/rc.d/interface_up_down.sh down


You can also test using the configctl which is what Cron calls.


# configctl interface_up_down down
# configctl interface_up_down up

OPNsense 25.7a - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

September 18, 2020, 07:45:36 PM #14 Last Edit: September 18, 2020, 07:52:25 PM by hirschferkel
Quote from: marjohn56 on September 18, 2020, 05:09:22 PM
the error you are seeing would suggest you haven't set the executable flags on the sh file, so check that.

Do you mean the 755 rights? I've set the rc.d like
drwxr-xr-x   2 root     wheel   1024 Sep 18 10:42 rc.d
and
-rwxr-xr-x  1 root  wheel    220 Sep 18 10:17 Interface_up_down.sh


# /usr/local/etc/rc.d/interface_up_down.sh down
-sh: /usr/local/etc/rc.d/interface_up_down.sh: not found

Even with the new file it does not work. I still get the 127 error?
Made a restart and
2020-09-18T19:45:00 configd.py[35743] [c0de5caa-ec09-4a6a-bc82-d7d9933daf0c] returned exit status 127

# configctl interface_up_down down
Error (127)


# /usr/local/etc/rc.d/interface_up_down.sh down
-sh: /usr/local/etc/rc.d/interface_up_down.sh: not found