技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 其他 --> 树莓派 Raspbian 家长控制

树莓派 Raspbian 家长控制

浏览:531次  出处信息

功能说明

  • 该脚本能够监控指定网络设备,如网络电视、机顶盒等,访问树莓派,不限服务(包括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

建议继续学习:

  1. 树莓派(Raspberry Pi)使用小记    (阅读:5966)
  2. 给树莓派安装ArchLinux    (阅读:3235)
  3. 如何将树莓派变成电子书服务器    (阅读:1199)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1