Turn On-Off Wifi with Atheros and Cron (archer C5/C7)

Post new topic   Reply to topic    DD-WRT Forum Index -> Atheros WiSOC based Hardware
Goto page Previous  1, 2, 3, 4, 5  Next
Author Message
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Tue Jun 06, 2017 0:20    Post subject: Reply with quote
Plazma, I think I found a bug on your script. Please confirm that:

If you adding just one slot, script doesn't work. You should add more than one slot.

START="1200"
STOP="2150"

this one doesn't work.

START="1200 2200"
STOP="2150 2201"

this one works.

_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
Sponsor
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Fri Jun 16, 2017 0:14    Post subject: Reply with quote
Plazma, Looks like I found a bug in your script. If you are using only one slot you should add a space to end of line like this:

Code:
START="1200 "
STOP="2140 "


If you are using more than one slot you should add a space after last slot like this:

Code:
START="1200 2145 2230 "
STOP="2140 2150 2235 "


For my tests definitly I should add a space. Please confirm that. Thanks.

_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
plazma
DD-WRT User


Joined: 22 Feb 2012
Posts: 68

PostPosted: Fri Jun 16, 2017 13:20    Post subject: PLAzmA WIFI Schedule Script Version 1.4 Reply with quote
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
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Fri Jun 16, 2017 14:18    Post subject: Reply with quote
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
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Fri Jun 16, 2017 14:24    Post subject: Reply with quote
Maybe I should add these commands for Wireless Client TURN ON section. I am not make sure. Currently I apply your code AS IS.

Code:
stopservice openvpn
sleep 5
startservice openvpn

_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
plazma
DD-WRT User


Joined: 22 Feb 2012
Posts: 68

PostPosted: Fri Jun 16, 2017 15:07    Post subject: Reply with quote
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.
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Fri Jun 16, 2017 15:13    Post subject: Reply with quote
plazma wrote:
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 Smile

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

#Might not be necessary...
startservice cron

_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
plazma
DD-WRT User


Joined: 22 Feb 2012
Posts: 68

PostPosted: Fri Jun 16, 2017 15:58    Post subject: Reply with quote
As per the change log the only change to the code was the fix for the single time slot.
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Sat Jun 17, 2017 4:50    Post subject: Reply with quote
plazma wrote:
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
war025
DD-WRT Novice


Joined: 10 Jul 2017
Posts: 2

PostPosted: Tue Jul 11, 2017 16:07    Post subject: isn't work for me Reply with quote
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.
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Tue Jul 11, 2017 16:14    Post subject: Re: isn't work for me Reply with quote
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
war025
DD-WRT Novice


Joined: 10 Jul 2017
Posts: 2

PostPosted: Sat Jul 15, 2017 17:09    Post subject: Re: isn't work for me Reply with quote
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!!!! Razz
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Sun Jul 16, 2017 9:55    Post subject: Reply with quote
You're welcome Wink
_________________
Kaan's World | @mkaand | PLEX Archive | Trakt.tv
bluecatus
DD-WRT Novice


Joined: 22 May 2017
Posts: 5

PostPosted: Sat Aug 12, 2017 8:50    Post subject: Reply with quote
Dear mkaand

If I have 02 radio (ath0 and ath1), how should I modify this script?

Thanks.
mkaand
DD-WRT User


Joined: 06 Jan 2008
Posts: 307
Location: Istanbul

PostPosted: Sat Aug 12, 2017 9:46    Post subject: Reply with quote
bluecatus wrote:
Dear mkaand

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
Goto page Previous  1, 2, 3, 4, 5  Next Display posts from previous:    Page 4 of 5
Post new topic   Reply to topic    DD-WRT Forum Index -> Atheros WiSOC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum