How to create a task in the openwrt system, the timer to switch WiFi power。

前言
在平时使用路由器过程中,晚上睡觉担心wifi的辐射会影响到健康(可能是心理作用吧),就想到能不能在晚上定时关闭WIFI,到白天又定时开启WIFI。明天就是五一劳动节了,可以晚起了。熬夜写了个脚本学习学习shell。

#!/bin/sh

#Timing shut down or open wifi script

fn=$1
fa=$#
#enable=""
close_time=" 00:00 12:00 " #定时关闭时间
open_time=" 08:00 14:00 "   #定时开启时间
timer_close=""
timer_open=""

timer_config="./timer_wifi.conf"
timer_log="/tmp/timer_wifi.log"

help_info()
{
    echo "
    1.E.G: $0 enable
    2.E.G: $0 disable
    "
}

init()
{

    if [ ! -e $timer_log ]
    then
        touch -c $timer_log
    fi
    if [ ! -e $timer_config ]
    then
        touch -c $timer_config
        #echo "enable=\"off"\">> $timer_config
        echo "`date` close_time=\"$close_time"\" >> $timer_config
        echo "`date` open_time=\"$open_time"\">> $timer_config
    else
    #  enable=`cat $timer_config | grep "enable" | awk -F = '{print $2}'`
       open_time=`cat $timer_config | grep "open_time" | awk -F = '{print $2}' | sed 's/"//g'`
       close_time=`cat $timer_config | grep "close_time" | awk -F = '{print $2}' | sed 's/"//g'`

       echo "
       Read from $timer_config
       [
            #enable=$enable
           open_time=$open_time
           close_time=$close_time
       ]
       "
    fi  
}

function()
{
    # if [ $fa -ne 1 ]
    # then
      # help_info
      # return 1
    # fi

    if [ $fn = "enable" ] 
    then
        enable="on"
        echo "`date` --------- enable=on" >> $timer_log
    elif [ $fn = "disable" ] 
    then
        enable="off"
        echo "`date` ---------  enable=off" >> $timer_log
    else
        help_info
    fi
#   return 0
}


init
function
while [ $enable = "on" ] 
do
    time_now=`date +%H:%M`
    #echo "time_now=$time_now"
    wifi_status=`ifconfig | grep wlan0`
     if [ -z "$wifi_status" ] #Check wifi status
     then
          for i in $open_time
          do
              if [ $time_now = $i ] ; then
                  echo "`date`---------- "wifi up" `wifi up` " >> $timer_log
              fi
          done
       else
           for i in $close_time
           do
               if [ $time_now = $i ] ; then
                   echo "`date`---------- "wifi down" `wifi down` " >> $timer_log
               fi
           done 
     fi
    sleep 10
done

脚本说明:
1,同时支持几个时间点,只需在/timer_wifi.conf

文件中添加或修改即可:

close_time=" 00:00 12:00 " 
open_time=" 08:00 14:00 "  

2,打开/关闭定时器方式:

./timer-wifi.sh enable/disable 

或 sh timer-wifi.sh enable/disable

3,每次更改参数或wifi状态,都会记录在/tmp/timer_wifi.log文件里;

Thu Apr 30 22:33:24 CST 2015 --------- enable=on
Thu Apr 30 22:34:04 CST 2015---------- wifi down  
Thu Apr 30 22:35:05 CST 2015---------- wifi up  
Thu Apr 30 22:36:05 CST 2015---------- wifi down  
Thu Apr 30 22:37:05 CST 2015---------- wifi up 

4,可以添加到开机后台运行,方法如下:

root@OpenWrt:/# cat /etc/rc.local 
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

sh /mnt/script/timer-wifi.sh enable &

exit 0
root@OpenWrt:/# reboot

已经运行了:

root@OpenWrt:/# ps
1719 root      1488 S    sh /mnt/script/timer-wifi.sh enable

到0点:关闭WIFI

root@OpenWrt:/mnt/script# date
Fri May  1 00:00:07 CST 2015
root@OpenWrt:/mnt/script#
 [  211.990000] device wlan0 left promiscuous mode
 [  212.000000] br-lan: port 2(wlan0) entered disabled state

root@OpenWrt:/tmp# cat timer_wifi.log 
Thu Apr 30 23:55:42 CST 2015 --------- enable=on
Fri May  1 00:00:07 CST 2015---------- wifi down  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值