Posted: Fri Jun 16, 2017 13:20 Post subject: PLAzmA WIFI Schedule Script Version 1.4
As mkaand pointed out the script had an issue when running with a single time slot, here is the updated and fixed code:
NOTE: there are two places where users must set variables, these are duplicated because one is used at boot and other other is used for the cron script, both must be updated accordingly.
Because the forum likes to add extra spaces at the end of line it may cause you an issue if you copy paste without removing them, due to this ive also added the file as an attachment to this post that you can download.
Code:
# PLAzmA WIFI Schedule Script Version 1.4
# SET USER VARIABLES PART 1
IFNAME=ath0
NTPSERVER=pool.ntp.org
# WRITE SCRIPT OUT TO TMP
cat > /tmp/wifi_cron_switch_run.sh << 'EOF'
#!/bin/sh
# SET USER VARIABLES PART 2
IFNAME=ath0
# ENTER AS MANY TIME SLOTS AS YOU REQUIRE, NOTE THESE MUST BE LISTED
# IN SMALLEST TO LARGEST, FOR EACH STAR A STOP IS REQUIRED
START="1000 1100 1200 1341 1844"
STOP="1010 1110 1210 1343 1845"
#### END OF USER VARIABLES ####
# IF BOOT HAS NOT COMPLETED DONT RUN
if [ ! -f /tmp/wifi_cron_switch_boot_complete ]; then exit 1; fi
# GET INTERFACE MODE
IFMODE=`nvram get $IFNAME\_mode`
# SET THE TIME ZONE
export TZ=`cat /etc/TZ`
# IF DD-WRT IS NOT SETTING THE TIME ZONE CORRECTLY, REPLACE USE BELOW INSTEAD
# MANUAL FIX FOR DAYLIGH SAVING TIME (REPLACE WITH CORRECT TIME ZONE)
# export TZ=GMT0BST,M3.5.0/1,M10.5.0
# SET INTERNAL VARS
COUNTER=0
SUBCOUNTER=0
WIFITOGGLE=0
# SINGLE TIME SLOT VAR CORRECTION
if [ ${#START} -le 4 ]; then START="$START "; fi
if [ ${#END} -le 4 ]; then END="$END "; fi
# CALCULATE THE TOTAL NUMBER OF TIME SLOTS
while [ "$TIMECHECK" != " " ]
do
COUNTER=$((COUNTER+1))
TOTALTIME=`echo "$START"| cut -d' ' -f$COUNTER`
TIMECHECK="$TOTALTIME "
done
COUNTER=$((COUNTER-1))
# COMPARE TIMES AND CHECK IF WIFI SHOULD BE ON OR OFF
while [ $SUBCOUNTER -lt $COUNTER ]
do
SUBCOUNTER=$((SUBCOUNTER+1))
SUBSTART=`echo "$START"| cut -d' ' -f$SUBCOUNTER`
SUBSTOP=`echo "$STOP"| cut -d' ' -f$SUBCOUNTER`
if [ $WIFITOGGLE -lt 2 ]; then
# WIFI OFF
if [ $(date +%H%M) -lt $SUBSTART ] || [ $(date +%H%M) -gt $SUBSTOP ]; then WIFITOGGLE=1; fi
# WIFI ON
if [ $(date +%H%M) -ge $SUBSTART ] && [ $(date +%H%M) -lt $SUBSTOP ]; then WIFITOGGLE=2; fi
fi
done
# IF WIRELESS IS IN CLIENT MODE TURN OFF
if [ "$IFMODE" = "sta" ] && [ "$WIFITOGGLE" = "1" ]; then
if [ $(ps | grep wpa_supplicant | cut -c27-40 | grep -v grep) = wpa_supplicant ]; then
stopservice wland >/dev/null 2>&1
killall wpa_supplicant
killall hostapd
mv /tmp/wifi_cron_switch_wifi_on /tmp/wifi_cron_switch_wifi_off >/dev/null 2>&1
fi
fi
# IF WIRELESS IS IN CLIENT MODE TURN ON
if [ "$IFMODE" = "sta" ] && [ "$WIFITOGGLE" = "2" ]; then
if [ "$(ps | grep wpa_supplicant | cut -c27-40 | grep -v grep)" != "wpa_supplicant" ]; then
stopservice lan
startservice lan
WIRELESSIEE=`cat /tmp/wifi_cron_switch_wpa_supplicant`
WPASUPPLICANTSTART="wpa_supplicant -B -$WIRELESSIEE -i$IFNAME -c /tmp/$IFNAME\_wpa_supplicant.conf"
$WPASUPPLICANTSTART >/dev/null 2>&1
sleep 10
killall udhcpc
UDHCPCSTART="udhcpc -i $IFNAME -p /var/run/udhcpc.pid -s /tmp/udhcpc -O routes -O msstaticroutes -O staticroutes"
$UDHCPCSTART >/dev/null 2>&1
mv /tmp/wifi_cron_switch_wifi_off /tmp/wifi_cron_switch_wifi_on >/dev/null 2>&1
fi
fi
# IF WIRELESS IS IN AP MODE TURN OFF
if [ "$IFMODE" = "ap" ] && [ "$WIFITOGGLE" = "1" ]; then
if [ $(ps | grep hostapd | cut -c27-33 | grep -v grep) = hostapd ]; then
stopservice wland >/dev/null 2>&1
killall hostapd
mv /tmp/wifi_cron_switch_wifi_on /tmp/wifi_cron_switch_wifi_off >/dev/null 2>&1
fi
fi
# IF WIRELESS IS IN AP MODE TURN ON
if [ "$IFMODE" = "ap" ] && [ "$WIFITOGGLE" = "2" ]; then
if [ "$(ps | grep hostapd | cut -c27-33 | grep -v grep)" != "hostapd" ]; then
stopservice lan >/dev/null 2>&1
startservice lan >/dev/null 2>&1
mv /tmp/wifi_cron_switch_wifi_off /tmp/wifi_cron_switch_wifi_on >/dev/null 2>&1
fi
fi
EOF
# SET THE EXECUTE BIT
chmod +x /tmp/wifi_cron_switch_run.sh
# GET INTERFACE MODE
IFMODE=`nvram get $IFNAME\_mode`
# CLIENT MODE CAPTURE WIFI COMMAND
if [ "$IFMODE" = "sta" ]; then
if [ ! -f /tmp/wifi_cron_switch_wpa_supplicant ]; then
ps | grep wpa | grep -v grep | awk -F "-" '/1/ {print $3}' > /tmp/wifi_cron_switch_wpa_supplicant
fi
fi
# WAIT UNTIL INTERFACE IS UP AND TIME CAN SYNC
while true; do ping -c1 $NTPSERVER > /dev/null && break; done
ntpclient $NTPSERVER
# SET THE ADAPTER DISCONNECTED STATE AND TRIGGER CRON SCRIPT TO RUN
stopservice wland; killall hostapd; killall wpa_supplicant; touch /tmp/wifi_cron_switch_wifi_off
touch /tmp/wifi_cron_switch_boot_complete
# DONT FORGET TO ADD THE CRONTAB JOB IN THE GUI (SANS QUOTES)
# "* * * * * root /bin/sh /tmp/wifi_cron_switch_run.sh"
#
# TO DEBUG:
# YOU CAN USE "date YYYYMMDDHHMM" IN TO QUICKLY ADJUST TIME TO TEST TIMES.
#
# CHANGE LOG:
#
# 1.4 FIXED ISSUE WITH SINGLE TIME SLOT
#
# 1.3 ADDED SUPPORT FOR MULTIPLE ON AND OFF TIMES, ADJUSTED DEBUG SYSTEM
# TO TOGGLE A SINGLE FILE WHICH WRITES OUT ONLY AFTER TOGGLE IS MADE,
# SUPPORT FOR CLIENT MODE AND AP MODE, SETS ADAPTOR BY INTERFACE NAME
# WHICH SHOULD ALLOW THE SCRIPT TO WORK ON OTHER CHIPSETS, AUTO TIMEZONE
# USING WEB INTERFACE SETTING, MANUAL SETTING COMMENTED OUT, INITIAL RUN
# WAITS UNTIL NTP SERVER RESPONDS TO PING AND SYNCS CLOCK BEFORE LOCKING
# THE WIRELESS DOWN, REMOVED REQUIREMENT TO SET WIRELESS MODE AS THIS IS
# NO LONGER TOGGLED.
#
# 1.2 ADDED DEBUG, PREVENT CRON SCHEDULE RUN UNTIL BOOT ENDS AND
# ENSURED SCRIPT WOULD STILL RUN IF /tmp BECOMES FULL
#
# 1.1 CHANGE TO USE SERVICE LAN FOR QUICKER RESTART
Hi Plazma, Thank you very much for fixing your code. Looks like you changed Wireles Client TURN ON condition too. I added only reboot command for TURN ON. As you know I had a problem because of OpenVPN client. Now I am not make sure your new code fixes OpenVPN Client issue. I will apply your code as is if I have an issue I replace again. Thank you. _________________ Kaan's World | @mkaand | PLEX Archive | Trakt.tv
The script as is has not got any default support for cycling a vpn, so if you made the modification previously you will need to also add that back in again.
The script as is has not got any default support for cycling a vpn, so if you made the modification previously you will need to also add that back in again.
Currently I have applied your code as is remotely. I mean the router is in my Garden and it is connected to my home. I applied v1.4 and it is working. First ping started then it stopped then it started again. (Turn ON condition) So looks like you fixed Open VPN cycle too. I use only one slot. 1200-2150
Just added these code from top of startup. I found this code in this forum. Looks like my cron is not stable and this code fixes cron. I am not make sure without this code your code can work or not. But you know Murphy's Law. If something is working don't touch
Code:
#!/bin/sh
#CronFix startup script
#Place in WebUI or {opt/jffs}/etc/config/____.startup
sleep 60
#Might not be necessary....
stopservice cron
#This one keep password same as root, duplicates Shell of root
[ -z `cat /tmp/etc/passwd | grep -o crontabs` ] && cat /tmp/etc/passwd | grep -m 1 root | sed 's/root/crontabs/'>> /tmp/etc/passwd
#This one leaves it blank, and is limited to OptWare. Change /opt/bin/bash to /bin/sh otherwise.
#grep -q "^crontabs:" /tmp/etc/passwd || echo 'crontabs:*:0:0:Contab User,,,:/tmp/root:/bin/sh' >> /tmp/etc/passwd
chmod 600 /tmp/crontab
chmod 750 /tmp/cron.d/
chmod 600 /tmp/cron.d/*
sleep 1
As per the change log the only change to the code was the fix for the single time slot.
Yes you right, I thought you changed Wireless Client Turn On section too. I compared it is same. My previous test (v1.3) I did to much changes maybe thats why it didn't work for Open VPN. Currently I am testing your code as is. If it is not working than I will apply my changes. (First boot wifi on and Turn On section just I added restart)
Thank you very much for your great effort. I hope one day DDWRT developers can I add your code to GUI and give you credits. _________________ Kaan's World | @mkaand | PLEX Archive | Trakt.tv
Posted: Tue Jul 11, 2017 16:07 Post subject: isn't work for me
Thanks for all plazma and mkaand. I'm very noob, I can't make work it on my WR841ND v7 with Atheros CPU. I follow this procedure:
I copy and paste the code (txt downloaded) with the modification START="0000 0700 1201"
STOP="0120 1200 2359" into Administration->Command Shell, I Save the Custom Script and then Run Commands, next I put * * * * * root /bin/sh /tmp/wifi_cron_switch_run.sh into Additional Cron Jobs to save and reboot but still not working.
Can you help me please?? what's wrong.. thank you.
Posted: Tue Jul 11, 2017 16:14 Post subject: Re: isn't work for me
war025 wrote:
Thanks for all plazma and mkaand. I'm very noob, I can't make work it on my WR841ND v7 with Atheros CPU. I follow this procedure:
I copy and paste the code (txt downloaded) with the modification START="0000 0700 1201"
STOP="0120 1200 2359" into Administration->Command Shell, I Save the Custom Script and then Run Commands, next I put * * * * * root /bin/sh /tmp/wifi_cron_switch_run.sh into Additional Cron Jobs to save and reboot but still not working.
Can you help me please?? what's wrong.. thank you.
P.D. I'm sorry but I speak a little English.
You're welcome. You should save this script as startup script not custom script. It'll work. _________________ Kaan's World | @mkaand | PLEX Archive | Trakt.tv
Posted: Sat Jul 15, 2017 17:09 Post subject: Re: isn't work for me
mkaand wrote:
war025 wrote:
Thanks for all plazma and mkaand. I'm very noob, I can't make work it on my WR841ND v7 with Atheros CPU. I follow this procedure:
I copy and paste the code (txt downloaded) with the modification START="0000 0700 1201"
STOP="0120 1200 2359" into Administration->Command Shell, I Save the Custom Script and then Run Commands, next I put * * * * * root /bin/sh /tmp/wifi_cron_switch_run.sh into Additional Cron Jobs to save and reboot but still not working.
Can you help me please?? what's wrong.. thank you.
P.D. I'm sorry but I speak a little English.
You're welcome. You should save this script as startup script not custom script. It'll work.
Mkaand, Thank you. That was the solution. Finally it's working!!!!
If I have 02 radio (ath0 and ath1), how should I modify this script?
Thanks.
Hi bluecatus, The script turns off all wireless radios. If you want to turn off just one radio, maybe script owner plazma can help you. _________________ Kaan's World | @mkaand | PLEX Archive | Trakt.tv