结合crond,每分钟判断,做家长管理。
crond的命令行是
* * * * * /etc/cron.d/parentcontrol.sh
下面是脚本
/etc/cron.d/parentcontrol.sh
-
#!/bin/sh
-
currentweek="`date +%w`"
-
currenthour="`date +%k`"
-
currentminute="`date +%M`"
-
#currentTime="`date +%k:%M`"
-
-
needFreshRule=0;
-
-
#weekIn(){
-
# weekdays=$1;
-
# let pos=`echo "$weekdays" | awk -F ''$currentweek'' '{printf "%d", length($0)-length($NF)}'`
-
# return $(($pos))
-
#}
-
-
-
timeCompare()
-
{
-
echo timeCompare: cur $currenthour:$currentminute vs $1:$2
-
time_h=$1;
-
time_m=$2;
-
if [ $currenthour -eq $time_h ] && [ $currentminute -eq $time_m ];then
-
return 2; #eq
-
elif [ "$currenthour" -eq "$time_h" ] && [ "$currentminute" -gt "$time_m" ];then
-
return 3; #gt
-
elif [ "$currenthour" -eq "$time_h" ] && [ "$currentminute" -lt "$time_m" ];then
-
return 1; #lt
-
elif [ "$currenthour" -lt "$time_h" ];then
-
return 1; #lt
-
elif [ "$currenthour" -gt "$time_h" ];then
-
return 3; #gt
-
fi
-
}
-
-
-
timeIn(){
-
echo timeIn: [ $1:$2 - $3:$4 ] $currenthour:"$currentminute"
-
timeS_h=$1;
-
timeS_m=$2;
-
timeE_h=$3;
-
timeE_m=$4;
-
-
vs_start=$(timeCompare $timeS_h $timeS_m)
-
echo $vs_start
-
vs_start=$?
-
vs_end=$(timeCompare $timeE_h $timeE_m)
-
echo $vs_end
-
vs_end=$?
-
-
#1 => "<" ; 2 => "=" ; 3 => ">"
-
-
if [ "$vs_start" -gt 1 ] && [ "$vs_end" -lt 3 ];then
-
return 1;
-
else
-
return 0;
-
fi
-
}
-
-
sum()
-
{
-
echo sum:$1,$2
-
return $(($1+$2));
-
}
-
-
checkFreshRulesDemand()
-
{
-
echo checkFreshRulesDemand:$1,$2,$3
-
weeks=$1;
-
Time_h=$2;
-
Time_m=$3;
-
-
isWeekIn=$(weekIn $weeks);
-
isWeekIn=$?;
-
-
if [ "$isWeekIn" -gt 0 ] ; then
-
if [ "$currenthour" -eq "$Time_h" ] && [ "$currentminute" -eq "$Time_m" ];then
-
return 1;
-
fi
-
fi
-
return 0;
-
}
-
-
addRules()
-
{
-
echo addRules: $1
-
mac=$1
-
ebtables -I PARENT_CONTROL -s $mac -j DROP
-
}
-
-
process_rule()
-
{
-
echo process_rule:$@
-
weeks=$1;
-
echo week=$weeks
-
macAddr=$2;
-
echo macAddr=$macAddr
-
T_en=$3;
-
echo T_en=$T_en
-
T_S_h=$4;
-
echo T_S_h=$T_S_h
-
T_S_m=$5;
-
echo T_S_m=$T_S_m
-
T_E_h=$6;
-
echo T_E_h=$T_E_h
-
T_E_m=$7;
-
echo T_E_m=$T_E_m
-
-
-
isWeekIn=$(weekIn $weeks);
-
isWeekIn=$?;
-
echo isWeekIn: $isWeekIn
-
if [ "$isWeekIn" -gt 0 ];then
-
if [ "$T_en" -gt 0 ];then
-
timein=$(timeIn $T_S_h $T_S_m $T_E_h $T_E_m);
-
echo $timein
-
timein=$?
-
echo timein:$timein
-
if [ "$timein" -gt 0 ];then
-
echo $(addRules $macAddr)
-
fi
-
fi
-
fi
-
}
-
-
-
rule1_week="1,2,3,4,5,6,7"
-
rule1_mac="11:22:33:44:55:66"
-
rule1_t1_enable="1"
-
rule1_t1_s_h=20
-
rule1_t1_s_m=10
-
rule1_t1_e_h=20
-
rule1_t1_e_m=50
-
rule1_t2_enable="1"
-
rule1_t2_s_h="08"
-
rule1_t2_s_m="10"
-
rule1_t2_e_h="20"
-
rule1_t2_e_m="50"
-
-
-
result=$(checkFreshRulesDemand $rule1_week $rule1_t1_s_h $rule1_t1_s_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
-
result=$(checkFreshRulesDemand $rule1_week $rule1_t1_e_h $rule1_t1_e_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
result=$(checkFreshRulesDemand $rule1_week $rule1_t2_s_h $rule1_t2_s_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
result=$(checkFreshRulesDemand $rule1_week $rule1_t2_e_h $rule1_t2_e_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
rule2_week="1,2,3,4,5,6,7"
-
rule2_mac="11:22:33:44:55:66"
-
rule2_t1_enable="1"
-
rule2_t1_s_h=11
-
rule2_t1_s_m=03
-
rule2_t1_e_h=20
-
rule2_t1_e_m=50
-
rule2_t2_enable="1"
-
rule2_t2_s_h=11
-
rule2_t2_s_m=02
-
rule2_t2_e_h=20
-
rule2_t2_e_m=50
-
-
-
result=$(checkFreshRulesDemand $rule2_week $rule2_t1_s_h $rule2_t1_s_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
result=$(checkFreshRulesDemand $rule2_week $rule2_t1_e_h $rule2_t1_e_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
result=$(checkFreshRulesDemand $rule2_week $rule2_t2_s_h $rule2_t2_s_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
result=$(checkFreshRulesDemand $rule2_week $rule2_t2_e_h $rule2_t2_e_m)
-
result=$?;
-
needFreshRule=$(sum $result $needFreshRule);
-
needFreshRule=$?;
-
-
-
echo 186,$needFreshRule;
-
needFreshRule=1;
-
-
if [ "$needFreshRule" -gt "0" ];then
-
ebtables -D INPUT -j PARENT_CONTROL
-
ebtables -X PARENT_CONTROL
-
ebtables -I INPUT -j PARENT_CONTROL
-
ebtables -N PARENT_CONTROL -P ACCEPT
-
ebtables -F PARENT_CONTROL
-
process_rule $rule1_week $rule1_mac $rule1_t1_enable $rule1_t1_s_h $rule1_t1_s_m $rule1_t1_e_h $rule1_t1_e_m
-
echo $process_rule
-
process_rule $rule1_week $rule1_mac $rule1_t2_enable $rule1_t2_s_h $rule1_t2_s_m $rule1_t2_e_h $rule1_t2_e_m
-
echo $process_rule
-
process_rule $rule2_week $rule2_mac $rule2_t1_enable $rule2_t1_s_h $rule2_t1_s_m $rule2_t1_e_h $rule2_t1_e_m
-
echo $process_rule
-
process_rule $rule2_week $rule2_mac $rule2_t2_enable $rule2_t2_s_h $rule2_t2_s_m $rule2_t2_e_h $rule2_t2_e_m
-
echo $process_rule
-
fi
阅读(1644) | 评论(0) | 转发(0) |