Chinaunix首页 | 论坛 | 博客
  • 博客访问: 755017
  • 博文数量: 119
  • 博客积分: 137
  • 博客等级: 少校
  • 技术积分: 1582
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-28 16:39
文章分类

全部博文(119)

文章存档

2017年(3)

2016年(7)

2014年(1)

2013年(8)

2012年(20)

2011年(27)

2010年(53)

分类: 网络与安全

2016-06-02 15:43:53

结合crond,每分钟判断,做家长管理。
crond的命令行是
* * * * * /etc/cron.d/parentcontrol.sh

下面是脚本
/etc/cron.d/parentcontrol.sh

点击(此处)折叠或打开

  1. #!/bin/sh
  2. currentweek="`date +%w`"
  3. currenthour="`date +%k`"
  4. currentminute="`date +%M`"
  5. #currentTime="`date +%k:%M`"
  6. needFreshRule=0;
  7. #weekIn(){
  8. # weekdays=$1;
  9. # let pos=`echo "$weekdays" | awk -F ''$currentweek'' '{printf "%d", length($0)-length($NF)}'`
  10. # return $(($pos))
  11. #}
  12. timeCompare()
  13. {
  14. echo timeCompare: cur $currenthour:$currentminute vs $1:$2
  15. time_h=$1;
  16. time_m=$2;
  17. if [ $currenthour -eq $time_h ] && [ $currentminute -eq $time_m ];then
  18. return 2; #eq
  19. elif [ "$currenthour" -eq "$time_h" ] && [ "$currentminute" -gt "$time_m" ];then
  20. return 3; #gt
  21. elif [ "$currenthour" -eq "$time_h" ] && [ "$currentminute" -lt "$time_m" ];then
  22. return 1; #lt
  23. elif [ "$currenthour" -lt "$time_h" ];then
  24. return 1; #lt
  25. elif [ "$currenthour" -gt "$time_h" ];then
  26. return 3; #gt
  27. fi
  28. }
  29. timeIn(){
  30. echo timeIn: [ $1:$2 - $3:$4 ] $currenthour:"$currentminute"
  31. timeS_h=$1;
  32. timeS_m=$2;
  33. timeE_h=$3;
  34. timeE_m=$4;
  35. vs_start=$(timeCompare $timeS_h $timeS_m)
  36. echo $vs_start
  37. vs_start=$?
  38. vs_end=$(timeCompare $timeE_h $timeE_m)
  39. echo $vs_end
  40. vs_end=$?
  41. #1 => "<" ; 2 => "=" ; 3 => ">"
  42. if [ "$vs_start" -gt 1 ] && [ "$vs_end" -lt 3 ];then
  43. return 1;
  44. else
  45. return 0;
  46. fi
  47. }
  48. sum()
  49. {
  50. echo sum:$1,$2
  51. return $(($1+$2));
  52. }
  53. checkFreshRulesDemand()
  54. {
  55. echo checkFreshRulesDemand:$1,$2,$3
  56. weeks=$1;
  57. Time_h=$2;
  58. Time_m=$3;
  59. isWeekIn=$(weekIn $weeks);
  60. isWeekIn=$?;
  61. if [ "$isWeekIn" -gt 0 ] ; then
  62. if [ "$currenthour" -eq "$Time_h" ] && [ "$currentminute" -eq "$Time_m" ];then
  63. return 1;
  64. fi
  65. fi
  66. return 0;
  67. }
  68. addRules()
  69. {
  70. echo addRules: $1
  71. mac=$1
  72. ebtables -I PARENT_CONTROL -s $mac -j DROP
  73. }
  74. process_rule()
  75. {
  76. echo process_rule:$@
  77. weeks=$1;
  78. echo week=$weeks
  79. macAddr=$2;
  80. echo macAddr=$macAddr
  81. T_en=$3;
  82. echo T_en=$T_en
  83. T_S_h=$4;
  84. echo T_S_h=$T_S_h
  85. T_S_m=$5;
  86. echo T_S_m=$T_S_m
  87. T_E_h=$6;
  88. echo T_E_h=$T_E_h
  89. T_E_m=$7;
  90. echo T_E_m=$T_E_m
  91. isWeekIn=$(weekIn $weeks);
  92. isWeekIn=$?;
  93. echo isWeekIn: $isWeekIn
  94. if [ "$isWeekIn" -gt 0 ];then
  95. if [ "$T_en" -gt 0 ];then
  96. timein=$(timeIn $T_S_h $T_S_m $T_E_h $T_E_m);
  97. echo $timein
  98. timein=$?
  99. echo timein:$timein
  100. if [ "$timein" -gt 0 ];then
  101. echo $(addRules $macAddr)
  102. fi
  103. fi
  104. fi
  105. }
  106. rule1_week="1,2,3,4,5,6,7"
  107. rule1_mac="11:22:33:44:55:66"
  108. rule1_t1_enable="1"
  109. rule1_t1_s_h=20
  110. rule1_t1_s_m=10
  111. rule1_t1_e_h=20
  112. rule1_t1_e_m=50
  113. rule1_t2_enable="1"
  114. rule1_t2_s_h="08"
  115. rule1_t2_s_m="10"
  116. rule1_t2_e_h="20"
  117. rule1_t2_e_m="50"
  118. result=$(checkFreshRulesDemand $rule1_week $rule1_t1_s_h $rule1_t1_s_m)
  119. result=$?;
  120. needFreshRule=$(sum $result $needFreshRule);
  121. needFreshRule=$?;
  122. result=$(checkFreshRulesDemand $rule1_week $rule1_t1_e_h $rule1_t1_e_m)
  123. result=$?;
  124. needFreshRule=$(sum $result $needFreshRule);
  125. needFreshRule=$?;
  126. result=$(checkFreshRulesDemand $rule1_week $rule1_t2_s_h $rule1_t2_s_m)
  127. result=$?;
  128. needFreshRule=$(sum $result $needFreshRule);
  129. needFreshRule=$?;
  130. result=$(checkFreshRulesDemand $rule1_week $rule1_t2_e_h $rule1_t2_e_m)
  131. result=$?;
  132. needFreshRule=$(sum $result $needFreshRule);
  133. needFreshRule=$?;
  134. rule2_week="1,2,3,4,5,6,7"
  135. rule2_mac="11:22:33:44:55:66"
  136. rule2_t1_enable="1"
  137. rule2_t1_s_h=11
  138. rule2_t1_s_m=03
  139. rule2_t1_e_h=20
  140. rule2_t1_e_m=50
  141. rule2_t2_enable="1"
  142. rule2_t2_s_h=11
  143. rule2_t2_s_m=02
  144. rule2_t2_e_h=20
  145. rule2_t2_e_m=50
  146. result=$(checkFreshRulesDemand $rule2_week $rule2_t1_s_h $rule2_t1_s_m)
  147. result=$?;
  148. needFreshRule=$(sum $result $needFreshRule);
  149. needFreshRule=$?;
  150. result=$(checkFreshRulesDemand $rule2_week $rule2_t1_e_h $rule2_t1_e_m)
  151. result=$?;
  152. needFreshRule=$(sum $result $needFreshRule);
  153. needFreshRule=$?;
  154. result=$(checkFreshRulesDemand $rule2_week $rule2_t2_s_h $rule2_t2_s_m)
  155. result=$?;
  156. needFreshRule=$(sum $result $needFreshRule);
  157. needFreshRule=$?;
  158. result=$(checkFreshRulesDemand $rule2_week $rule2_t2_e_h $rule2_t2_e_m)
  159. result=$?;
  160. needFreshRule=$(sum $result $needFreshRule);
  161. needFreshRule=$?;
  162. echo 186,$needFreshRule;
  163. needFreshRule=1;
  164. if [ "$needFreshRule" -gt "0" ];then
  165. ebtables -D INPUT -j PARENT_CONTROL
  166. ebtables -X PARENT_CONTROL
  167. ebtables -I INPUT -j PARENT_CONTROL
  168. ebtables -N PARENT_CONTROL -P ACCEPT
  169. ebtables -F PARENT_CONTROL
  170. 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
  171. echo $process_rule
  172. 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
  173. echo $process_rule
  174. 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
  175. echo $process_rule
  176. 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
  177. echo $process_rule
  178. fi

阅读(1652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~