树莓派 Raspbian 家长控制
浏览:1802次 出处信息
功能说明
该脚本能够监控指定网络设备,如网络电视、机顶盒等,访问树莓派,不限服务(包括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 --comment $2 -m mac --mac-source $1 -j DROP
}
clean()
{
echo Clean for $1
j=0
for i in `iptables -nL --line-numbers | grep $1 | cut -d -f1` ; do
echo Remove line $i
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 $User1 $User2 $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-source $mac -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=$(echo $i | cut -f 1 -d_)
t=$(echo $i | cut -f 2 -d_)
mac=$(echo $i | cut -f 3 -d_)
if [ "$now" -ge "$t" ] ;then
if [ "$a" == "block" ] ;then
block $mac 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
建议继续学习:
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:视频的容器与格式
后一篇:如何迁移一个Git仓库 >>
文章信息
- 作者:lesca 来源: Lesca 技术宅
- 标签: raspbian 家长控制 树莓派
- 发布时间:2020-02-01 19:50:55
近3天十大热文
-
[938] WordPress插件开发 -- 在插件使用 -
[119] 解决 nginx 反向代理网页首尾出现神秘字 -
[51] 如何保证一个程序在单台服务器上只有唯一实例( -
[50] ps 命令常见用法 -
[49] 用 Jquery 模拟 select -
[49] 整理了一份招PHP高级工程师的面试题 -
[49] 海量小文件存储 -
[48] find命令的一点注意事项 -
[48] Innodb分表太多或者表分区太多,会导致内 -
[47] 全站换域名时利用nginx和javascri
