树莓派 Raspbian 家长控制
功能说明
- 该脚本能够监控指定网络设备,如网络电视、机顶盒等,访问树莓派,不限服务(包括SMB, NFS等)。典型场景是机顶盒访问树莓派文件共享服务。
- 该脚本使网络设备只能在一段时间内,默认1小时,访问树莓派,之后冷却1小时,以防止过度访问。
- 该脚本需要经过需改,才可以部署到OpenWRT等依赖
ash
的网络设备中。
准备工作
改下面两个文件:
- 一是将需要监测的设备的MAC地址,比如网络电视,添加到
iptables
日志监控中。 - 二是将日志重定向到一个叫做
iptables.log
的日志文件,方便监控进程监控。代码须放置在rsyslog.conf
顶部。
#/etc/rc.local iptables -I INPUT 1 -m mac --mac-source c8:0e:77:xx:xx:xx -j LOG --log-prefix "ConnectionRequest: " # /etc/rsyslog.conf :msg,contains,"ConnectionRequest: " -/var/log/iptables.log & ~
用法
创建下面的脚本,命名为timeGuard
,添加执行权限,分别运行:
- timeGuard /log &
创建日志监控进程,该进程根据iptables.log
中的内容监控客户端访问树莓派状况,被监控设备必须同时出现在iptables
监控列表,及下面的脚本中。 - timeGuard /time &
创建时间触发进程,该进程每15秒检测/tmp/timeGuard/
中的文件,执行其所描述操作
脚本
/!\ 该脚本版权归Lesca技术宅所有,转载、分发、使用请注明出处。
#/bin/ash # Copyright (C) Lesca Oct, 2017 # Init User1=c8:0e:77:xx:xx:xx User2=c8:0e:77:xx:xx:xx User3=c8:0e:77:xx:xx:xx block() { echo Block 2 iptables -I INPUT -p tcp -m comment --comment2 -m mac --mac-source 1 -j DROP } clean() { echo Clean for1 j=0 for i in `iptables -nL --line-numbers | grep 1 | cut -d' ' -f1` ; do echo Remove linei iptables -D INPUT ((i-$j)) let j++ done } logGuard() { [ ! -e /tmp/timeGuard/ ] && mkdir /tmp/timeGuard tail -n0 -f /var/log/iptables.log | while read logline; do for mac in User1User2 $User3 ;do if [ -z "{logline##*ConnectionRequest*mac*}" ] ;then t0=(date +"%Y%m%d%H%M") t1=(date +"%Y%m%d%H%M" -d "+60min") t2=$(date +"%Y%m%d%H%M" -d "+120min") if [ `ls /tmp/timeGuard/*{mac} 2>/dev/null | wc -l` -eq 0 ] ;then echo Intenet access logged:mac t0 iptables -I INPUT 1 -m mac --mac-sourcemac -m comment --comment timeGuard_{mac} -j ACCEPT touch /tmp/timeGuard/block_{t1}_{mac} touch /tmp/timeGuard/clean_{t2}_{mac} fi fi done done } timeGuard() { while true ;do now=(date +"%Y%m%d%H%M") for i in `ls /tmp/timeGuard/` ;do a=(echoi | cut -f 1 -d_) t=(echoi | cut -f 2 -d_) mac=(echoi | cut -f 3 -d_) if [ "now" -ge "t" ] ;then if [ "a" == "block" ] ;then blockmac timeGuard_{mac} rm -f /tmp/timeGuard/i elif [ "a" == "clean" ] ;then clean timeGuard_{mac} rm -f /tmp/timeGuard/i fi fi done sleep 15 done } if [ "1" != "" ]; then [[ "1" == "logGuard" || "1" == "log" ]] && logGuard && exit [[ "1" == "timeGuard" || "1" == "time" ]] && timeGuard && exit fi
Copyright
本文出自 Lesca 技术宅,转载时请注明出处及相应链接。
本文永久链接: https://lesca.me/archives/raspberrypi-raspbian-parental-control.html
3 Comments
哈哈,现在的文章我都快看不懂了。真是技术宅啊,2333
术业有专攻嘛 各有所长吧 2333
现在在做什么工作呢?和这些是不是关系很密切?(另,麻烦更新下侧栏的链接,更换域名简直要重新做人了)